質問編集履歴
5
post
test
CHANGED
File without changes
|
test
CHANGED
@@ -314,10 +314,80 @@
|
|
314
314
|
|
315
315
|
|
316
316
|
|
317
|
+
class PostsController < ApplicationController
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
before_action :authenticate_user
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
before_action :ensure_correct_user,{only:[:edit,:update,:destroy]}
|
326
|
+
|
327
|
+
|
328
|
+
|
317
|
-
def e
|
329
|
+
def index
|
330
|
+
|
331
|
+
@posts = Post.all.order(created_at: :desc)
|
332
|
+
|
333
|
+
end
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
def show
|
318
338
|
|
319
339
|
@post = Post.find_by(id: params[:id])
|
320
340
|
|
341
|
+
# 変数@userを定義してください
|
342
|
+
|
343
|
+
@user = User.find_by(id: @post.user_id)
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
end
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
def new
|
352
|
+
|
353
|
+
@post = Post.new
|
354
|
+
|
355
|
+
end
|
356
|
+
|
357
|
+
|
358
|
+
|
359
|
+
def create
|
360
|
+
|
361
|
+
@post = Post.new(
|
362
|
+
|
363
|
+
content:params[:content],
|
364
|
+
|
365
|
+
user_id: @current_user.id
|
366
|
+
|
367
|
+
)
|
368
|
+
|
369
|
+
if @post.save
|
370
|
+
|
371
|
+
redirect_to("/posts/index")
|
372
|
+
|
373
|
+
else
|
374
|
+
|
375
|
+
render("posts/new")
|
376
|
+
|
377
|
+
end
|
378
|
+
|
379
|
+
end
|
380
|
+
|
381
|
+
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
def edit
|
388
|
+
|
389
|
+
@post = Post.find_by(id: params[:id])
|
390
|
+
|
321
391
|
end
|
322
392
|
|
323
393
|
|
@@ -344,4 +414,110 @@
|
|
344
414
|
|
345
415
|
end
|
346
416
|
|
417
|
+
|
418
|
+
|
419
|
+
def destroy
|
420
|
+
|
421
|
+
@post = Post.find_by(id: params[:id])
|
422
|
+
|
423
|
+
@post.destroy
|
424
|
+
|
425
|
+
redirect_to("/posts/index")
|
426
|
+
|
427
|
+
end
|
428
|
+
|
429
|
+
|
430
|
+
|
431
|
+
def ensure_correct_user
|
432
|
+
|
433
|
+
@post = Post.find_by(id: params[:id])
|
434
|
+
|
435
|
+
if @post.user_id != @current_user.id
|
436
|
+
|
437
|
+
flash[:notice]= "権限がありません"
|
438
|
+
|
439
|
+
redirect_to("/posts/index")
|
440
|
+
|
441
|
+
end
|
442
|
+
|
443
|
+
end
|
444
|
+
|
445
|
+
|
446
|
+
|
447
|
+
end
|
448
|
+
|
449
|
+
|
450
|
+
|
347
|
-
```
|
451
|
+
```
|
452
|
+
|
453
|
+
|
454
|
+
|
455
|
+
```
|
456
|
+
|
457
|
+
**new.html**
|
458
|
+
|
459
|
+
<div class="edit-wrapper">
|
460
|
+
|
461
|
+
<div class="cont">
|
462
|
+
|
463
|
+
<div class="edit-wrapper-main">
|
464
|
+
|
465
|
+
<div class="edit-title">
|
466
|
+
|
467
|
+
<h1>今日の積み上げ</h1>
|
468
|
+
|
469
|
+
</div>
|
470
|
+
|
471
|
+
<div class="edit-contents">
|
472
|
+
|
473
|
+
|
474
|
+
|
475
|
+
<div class="edit-content edit-1">
|
476
|
+
|
477
|
+
<p>今日の積み上げ時間</p>
|
478
|
+
|
479
|
+
<p><textarea class="edit-time" type="text" name="" value=""> </textarea>分</P>
|
480
|
+
|
481
|
+
</div>
|
482
|
+
|
483
|
+
|
484
|
+
|
485
|
+
<div class="edit-content edit-2">
|
486
|
+
|
487
|
+
<p>今日やったこと</p>
|
488
|
+
|
489
|
+
<textarea class="edit-thing" type="text" name="" value=""> </textarea>
|
490
|
+
|
491
|
+
</div>
|
492
|
+
|
493
|
+
|
494
|
+
|
495
|
+
<div class="edit-content">
|
496
|
+
|
497
|
+
<p>具体的な内容</p>
|
498
|
+
|
499
|
+
<%= form_tag("/posts/create") do %>
|
500
|
+
|
501
|
+
<textarea name="content" rows="8" cols="80"> <%= @post.content %> </textarea>
|
502
|
+
|
503
|
+
<% end %>
|
504
|
+
|
505
|
+
</div>
|
506
|
+
|
507
|
+
|
508
|
+
|
509
|
+
|
510
|
+
|
511
|
+
|
512
|
+
|
513
|
+
</div>
|
514
|
+
|
515
|
+
<input class="edit-submit" type="submit" name="" value="投稿">
|
516
|
+
|
517
|
+
</div>
|
518
|
+
|
519
|
+
</div>
|
520
|
+
|
521
|
+
</div>
|
522
|
+
|
523
|
+
```
|
4
postの編集機能の追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -303,3 +303,45 @@
|
|
303
303
|
end
|
304
304
|
|
305
305
|
```
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
postの編集機能の部分のコードです↓
|
310
|
+
|
311
|
+
```
|
312
|
+
|
313
|
+
**contorollers - post_controller**
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
def edit
|
318
|
+
|
319
|
+
@post = Post.find_by(id: params[:id])
|
320
|
+
|
321
|
+
end
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
def update
|
326
|
+
|
327
|
+
@post = Post.find_by(id: params[:id])
|
328
|
+
|
329
|
+
@post.content = params[:content]
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
if @post.save
|
334
|
+
|
335
|
+
redirect_to("/posts/index")
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
else
|
340
|
+
|
341
|
+
render("posts/edit")
|
342
|
+
|
343
|
+
end
|
344
|
+
|
345
|
+
end
|
346
|
+
|
347
|
+
```
|
3
user.rbの修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -248,19 +248,25 @@
|
|
248
248
|
|
249
249
|
|
250
250
|
|
251
|
+
|
252
|
+
|
251
253
|
class User < ApplicationRecord
|
252
254
|
|
255
|
+
# has_secure_passwordメソッドを追加してください
|
256
|
+
|
257
|
+
has_secure_password
|
258
|
+
|
253
259
|
|
254
260
|
|
255
|
-
|
261
|
+
validates :name, {presence: true}
|
262
|
+
|
263
|
+
validates :email, {presence: true, uniqueness: true}
|
264
|
+
|
265
|
+
|
256
266
|
|
257
267
|
|
258
268
|
|
259
|
-
|
269
|
+
has_many :posts
|
260
|
-
|
261
|
-
validates :email, {presence: true, uniqueness: true}
|
262
|
-
|
263
|
-
|
264
270
|
|
265
271
|
|
266
272
|
|
2
post.rbの追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -292,6 +292,8 @@
|
|
292
292
|
|
293
293
|
|
294
294
|
|
295
|
+
belongs_to :user
|
296
|
+
|
295
297
|
end
|
296
298
|
|
297
299
|
```
|
1
modelのコードの加筆
test
CHANGED
File without changes
|
test
CHANGED
@@ -235,3 +235,63 @@
|
|
235
235
|
|
236
236
|
|
237
237
|
https://prog-8.com/rails5/study/9/4#/11
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
### 追記(modelのコード)
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
```
|
246
|
+
|
247
|
+
**model - user.rb**
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
class User < ApplicationRecord
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
has_secure_password
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
validates :name, {presence: true}
|
260
|
+
|
261
|
+
validates :email, {presence: true, uniqueness: true}
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
def posts
|
268
|
+
|
269
|
+
return Post.where(user_id: self.id)
|
270
|
+
|
271
|
+
end
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
end
|
276
|
+
|
277
|
+
```
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
```
|
282
|
+
|
283
|
+
**model - post.rb**
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
class Post < ApplicationRecord
|
288
|
+
|
289
|
+
validates :content, {presence: true ,length:{maximum:140}}
|
290
|
+
|
291
|
+
validates :user_id,{presence:true}
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
end
|
296
|
+
|
297
|
+
```
|