質問編集履歴

5

toppages.controllerの修正

2018/01/26 12:31

投稿

begin1990
begin1990

スコア31

test CHANGED
File without changes
test CHANGED
@@ -24,7 +24,9 @@
24
24
 
25
25
  @posts = current_user.feed_posts.order('created_at DESC').page(params[:page])
26
26
 
27
- #@comment = @post.comments.create
27
+ @comment = @post.comments.build
28
+
29
+ @comments = @post.comments.order('created_at DESC').page(params[:page])
28
30
 
29
31
  end
30
32
 
@@ -74,6 +76,12 @@
74
76
 
75
77
  else
76
78
 
79
+ @comments = @comments.order('created_at DESC').page(params[:page])
80
+
81
+ flash.now[:danger] = 'コメントの投稿に失敗しました。
82
+
83
+
84
+
77
85
  render 'toppages/index'
78
86
 
79
87
  end

4

<form> タグの部分(ブラウザーで HTML ソースを表示してその HTML 断片)を追加

2018/01/26 12:31

投稿

begin1990
begin1990

スコア31

test CHANGED
File without changes
test CHANGED
@@ -462,6 +462,24 @@
462
462
 
463
463
 
464
464
 
465
+ /posts を表示したときのコメントのフォームの <form> タグの部分(ブラウザーで HTML ソースを表示してその HTML 断片)
466
+
467
+ ```
468
+
469
+ <form class="new_comment" id="new_comment" action="/posts/5/comments" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="&#x2713;" /><input type="hidden" name="authenticity_token" value="4i0OQgfiY2xoD/pIvEJI9XtKhTfJnyjRfZAVOVJ9i9wvQSHu/zWiXB7b6HA35FG0AdkwxgS4AgM2jPEkA+r+Ug==" />
470
+
471
+ <textarea cols="30" placeholder="コメントする" rows="2" name="comment[content]" id="comment_content">
472
+
473
+ </textarea>
474
+
475
+ <input type="submit" name="commit" value="コメントの投稿" data-disable-with="コメントの投稿" />
476
+
477
+ </form>
478
+
479
+ ```
480
+
481
+
482
+
465
483
 
466
484
 
467
485
  やりたいこと:トップページに表示されているPostにコメントを投稿する。

3

ルーティングの修正

2018/01/23 10:47

投稿

begin1990
begin1990

スコア31

test CHANGED
File without changes
test CHANGED
@@ -444,7 +444,7 @@
444
444
 
445
445
 
446
446
 
447
- resources :posts, only: [:create, :destroy, :show, :new] , shallow: true do
447
+ resources :posts, only: [:create, :destroy, :show] , shallow: true do
448
448
 
449
449
  resources :comments, only: [:create, :destroy]
450
450
 

2

ルーティングの追加

2018/01/22 15:31

投稿

begin1990
begin1990

スコア31

test CHANGED
File without changes
test CHANGED
@@ -404,6 +404,62 @@
404
404
 
405
405
  ```
406
406
 
407
+ ルーティング
408
+
409
+ routes.rb
410
+
411
+ ```
412
+
413
+ Rails.application.routes.draw do
414
+
415
+ root to: 'toppages#index'
416
+
417
+
418
+
419
+ get 'login', to: 'sessions#new'
420
+
421
+ post 'login', to: 'sessions#create'
422
+
423
+ delete 'logout', to: 'sessions#destroy'
424
+
425
+
426
+
427
+ resources :users do
428
+
429
+ member do
430
+
431
+ get :followings
432
+
433
+ get :followers
434
+
435
+ end
436
+
437
+ collection do
438
+
439
+ get :search
440
+
441
+ end
442
+
443
+ end
444
+
445
+
446
+
447
+ resources :posts, only: [:create, :destroy, :show, :new] , shallow: true do
448
+
449
+ resources :comments, only: [:create, :destroy]
450
+
451
+ end
452
+
453
+
454
+
455
+ resources :relationships, only: [:create, :destroy]
456
+
457
+ get 'signup', to: 'users#new'
458
+
459
+ end
460
+
461
+ ```
462
+
407
463
 
408
464
 
409
465
 

1

toppage_controllerの追加

2018/01/22 15:28

投稿

begin1990
begin1990

スコア31

test CHANGED
File without changes
test CHANGED
@@ -8,6 +8,34 @@
8
8
 
9
9
  コードは以下の通りです。
10
10
 
11
+ toppage_controller
12
+
13
+ ```
14
+
15
+ class ToppagesController < ApplicationController
16
+
17
+ def index
18
+
19
+ if logged_in?
20
+
21
+ @user = current_user
22
+
23
+ @post = current_user.posts.build # form_for 用
24
+
25
+ @posts = current_user.feed_posts.order('created_at DESC').page(params[:page])
26
+
27
+ #@comment = @post.comments.create
28
+
29
+ end
30
+
31
+ end
32
+
33
+ end
34
+
35
+ ```
36
+
37
+
38
+
11
39
  comment_controller
12
40
 
13
41
  ```