質問編集履歴
8
ファイルパスの追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -120,7 +120,7 @@
|
|
120
120
|
end
|
121
121
|
```
|
122
122
|
|
123
|
-
comments.html.erb
|
123
|
+
comments/_comments.html.erb
|
124
124
|
```
|
125
125
|
<ul class="media-list">
|
126
126
|
<% @post_comments.each do |post_comment| %>
|
@@ -139,7 +139,7 @@
|
|
139
139
|
</ul>
|
140
140
|
```
|
141
141
|
|
142
|
-
posts.html.erb
|
142
|
+
posts/_posts.html.erb
|
143
143
|
```
|
144
144
|
<ul class="media-list">
|
145
145
|
<% posts.each do |post| %>
|
7
本文修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -210,8 +210,8 @@
|
|
210
210
|
|
211
211
|
toppages_controller.rbに
|
212
212
|
```
|
213
|
-
@
|
213
|
+
@post_comment = current_user.post_comments.build
|
214
|
-
@
|
214
|
+
@post_comments = @post.post_comments.order('created_at DESC').page(params[:page])
|
215
215
|
```
|
216
216
|
を入れれば表示されると思っていましたが、全く表示されませんでした。
|
217
217
|
|
6
本文の修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -208,7 +208,7 @@
|
|
208
208
|
<% end %>
|
209
209
|
```
|
210
210
|
|
211
|
-
|
211
|
+
toppages_controller.rbに
|
212
212
|
```
|
213
213
|
@comment = @post.comments.build
|
214
214
|
@comments = @post.comments.order('created_at DESC').page(params[:page])
|
5
テンプレートパスの追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
[https://gyazo.com/e07751929a071e6d52390b314ae87ae0](https://gyazo.com/e07751929a071e6d52390b314ae87ae0)の画面で、コメント投稿をしたところ、コメントがトップページに表示されません(コンソール上ではコメントが保存されいています)。
|
6
6
|
|
7
7
|
現在のコードは以下の通りです。
|
8
|
-
toppages.controller
|
8
|
+
toppages.controller.rb
|
9
9
|
```
|
10
10
|
class ToppagesController < ApplicationController
|
11
11
|
def index
|
@@ -20,8 +20,9 @@
|
|
20
20
|
end
|
21
21
|
```
|
22
22
|
|
23
|
+
|
23
|
-
|
24
|
+
posts_controller.rb
|
24
|
-
```
|
25
|
+
```
|
25
26
|
class PostsController < ApplicationController
|
26
27
|
before_action :require_user_logged_in
|
27
28
|
before_action :correct_user, only: [:destroy]
|
@@ -67,7 +68,7 @@
|
|
67
68
|
end
|
68
69
|
```
|
69
70
|
|
70
|
-
|
71
|
+
post_comments.controller.rb
|
71
72
|
```
|
72
73
|
class PostCommentsController < ApplicationController
|
73
74
|
before_action :require_user_logged_in
|
@@ -119,7 +120,7 @@
|
|
119
120
|
end
|
120
121
|
```
|
121
122
|
|
122
|
-
comments
|
123
|
+
comments.html.erb
|
123
124
|
```
|
124
125
|
<ul class="media-list">
|
125
126
|
<% @post_comments.each do |post_comment| %>
|
@@ -138,7 +139,7 @@
|
|
138
139
|
</ul>
|
139
140
|
```
|
140
141
|
|
141
|
-
posts
|
142
|
+
posts.html.erb
|
142
143
|
```
|
143
144
|
<ul class="media-list">
|
144
145
|
<% posts.each do |post| %>
|
@@ -175,7 +176,7 @@
|
|
175
176
|
</ul>
|
176
177
|
```
|
177
178
|
|
178
|
-
|
179
|
+
toppages/index.html.erb
|
179
180
|
```
|
180
181
|
<% if logged_in? %>
|
181
182
|
<div class="row">
|
4
コントローラの修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -13,6 +13,8 @@
|
|
13
13
|
@user = current_user
|
14
14
|
@post = current_user.posts.build # form_for 用
|
15
15
|
@posts = current_user.feed_posts.order('created_at DESC').page(params[:page])
|
16
|
+
@post_comment = current_user.post_comments.build
|
17
|
+
@post_comments = @post.post_comments.order('created_at DESC').page(params[:page])
|
16
18
|
end
|
17
19
|
end
|
18
20
|
end
|
@@ -71,11 +73,6 @@
|
|
71
73
|
before_action :require_user_logged_in
|
72
74
|
before_action :correct_user, only: [:destroy]
|
73
75
|
|
74
|
-
def index
|
75
|
-
@post = Post.find(params[:post_id])
|
76
|
-
@post_comments = @post.post_comments.order('created_at DESC').page(params[:page])
|
77
|
-
end
|
78
|
-
|
79
76
|
def create
|
80
77
|
@post = Post.find(params[:post_id])
|
81
78
|
@post_comment = @post.post_comments.create(post_comment_params)
|
3
本文修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -25,7 +25,7 @@
|
|
25
25
|
before_action :correct_user, only: [:destroy]
|
26
26
|
|
27
27
|
def index
|
28
|
-
@
|
28
|
+
@post_comment = current_user.post_comments.build
|
29
29
|
end
|
30
30
|
|
31
31
|
def show
|
@@ -217,5 +217,6 @@
|
|
217
217
|
```
|
218
218
|
を入れれば表示されると思っていましたが、全く表示されませんでした。
|
219
219
|
|
220
|
+
|
220
221
|
他にも色々試しましたが、それでもコメントは表示されませんでした。
|
221
222
|
これ以上、トップページに投稿したコメントを表示させる方法がわからないので、どなたかご教示をお願いできませんか?
|
2
本文の修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -13,7 +13,6 @@
|
|
13
13
|
@user = current_user
|
14
14
|
@post = current_user.posts.build # form_for 用
|
15
15
|
@posts = current_user.feed_posts.order('created_at DESC').page(params[:page])
|
16
|
-
@comments = @post.comments.order('created_at DESC').page(params[:page])
|
17
16
|
end
|
18
17
|
end
|
19
18
|
end
|
@@ -68,62 +67,77 @@
|
|
68
67
|
|
69
68
|
comments.controller
|
70
69
|
```
|
71
|
-
class
|
70
|
+
class PostCommentsController < ApplicationController
|
72
|
-
#before_action :set_comment, only: [:new,:create, :destroy]
|
73
71
|
before_action :require_user_logged_in
|
72
|
+
before_action :correct_user, only: [:destroy]
|
74
73
|
|
74
|
+
def index
|
75
|
+
@post = Post.find(params[:post_id])
|
76
|
+
@post_comments = @post.post_comments.order('created_at DESC').page(params[:page])
|
77
|
+
end
|
78
|
+
|
75
79
|
def create
|
76
80
|
@post = Post.find(params[:post_id])
|
77
|
-
@
|
81
|
+
@post_comment = @post.post_comments.create(post_comment_params)
|
78
|
-
@
|
82
|
+
@post_comment.user_id = current_user.id
|
79
|
-
#@
|
83
|
+
#@post_comment = current_user.post_comments.build(post_comment_params)
|
80
84
|
#@comment = Comment.create(text: comment_params[:text], post_id: comment_params[:post_id], user_id: current_user.id)
|
81
|
-
if @
|
85
|
+
if @post_comment.save
|
82
86
|
flash[:success] = "コメントしました。"
|
83
87
|
#redirect_to "/posts/#{@comment.post.id}"
|
84
88
|
#redirect_to post_comments_path(@post.id)
|
85
89
|
#redirect_to :action =>"new"
|
86
90
|
redirect_to root_url
|
87
91
|
else
|
88
|
-
@
|
92
|
+
@post_comments = @post.post_comments.order('created_at DESC').page(params[:page])
|
89
93
|
flash.now[:danger] = 'コメントの投稿に失敗しました。'
|
90
94
|
render 'toppages/index'
|
91
95
|
end
|
92
96
|
end
|
93
97
|
|
94
98
|
def destroy
|
95
|
-
@
|
99
|
+
@post_comment.destroy
|
96
100
|
flash[:success] = 'コメントを削除しました。'
|
97
101
|
redirect_back(fallback_location: root_path)
|
98
102
|
end
|
99
103
|
|
100
104
|
private
|
101
105
|
# Use callbacks to share common setup or constraints between actions.
|
102
|
-
|
106
|
+
def set_post_comment
|
103
|
-
|
107
|
+
@post = Post.find(params[:post_id])
|
104
|
-
|
108
|
+
@post_comment = @post.post_comments.find(params[:id])
|
105
|
-
|
109
|
+
end
|
106
110
|
|
107
111
|
# Never trust parameters from the scary internet, only allow the white list through.
|
108
|
-
|
112
|
+
def post_comment_params
|
109
|
-
|
113
|
+
params.require(:post_comment).permit(:user_id, :post_id, :content)
|
114
|
+
end
|
115
|
+
|
116
|
+
def correct_user
|
117
|
+
@post_comment = current_user.post_comments.find_by(id: params[:id])
|
118
|
+
unless @post_comment
|
119
|
+
redirect_to root_path
|
110
120
|
end
|
121
|
+
end
|
111
122
|
end
|
112
123
|
```
|
113
124
|
|
114
125
|
commentsビュー
|
115
126
|
```
|
116
127
|
<ul class="media-list">
|
117
|
-
<% @
|
128
|
+
<% @post_comments.each do |post_comment| %>
|
118
|
-
<div class="name2">投稿者:<%= link_to comment.user, "/users/#{comment.user_id}" %> 投稿日時:<%= comment.created_at.strftime("%Y-%m-%d %H:%M:%S") %></div>
|
119
|
-
|
129
|
+
<% user = post_comment.user %>
|
120
130
|
<div>
|
131
|
+
<p>投稿者:<%= link_to user.name, user_path(user) %> <span class="text-muted">commented at <%= post_comment.created_at %></span></p>
|
132
|
+
</div>
|
133
|
+
<p><%= post_comment.content %></p>
|
134
|
+
<div>
|
121
|
-
<% if current_user ==
|
135
|
+
<% if current_user == post_comment.user %>
|
122
|
-
<%= link_to "削除",
|
136
|
+
<%= link_to "削除", post_comment, method: :delete, data: { confirm: "本当に削除してよろしいですか?" }, class: 'btn btn-danger btn-sm' %>
|
123
137
|
<% end %>
|
124
138
|
</div>
|
125
139
|
<% end %>
|
126
|
-
<%= paginate
|
140
|
+
<%= paginate @post_comments %>
|
127
141
|
</ul>
|
128
142
|
```
|
129
143
|
|
@@ -143,7 +157,7 @@
|
|
143
157
|
<div>
|
144
158
|
<p><%= image_tag post.picture,:size =>"280x210" %></p>
|
145
159
|
<p><%= post.content %></p>
|
146
|
-
<%= render 'comments/comments',
|
160
|
+
<%= render 'comments/comments', post_comments: @post_comments %>
|
147
161
|
<br/>
|
148
162
|
<% if current_user %>
|
149
163
|
<%= form_for [post, Comment.new] do |form| %>
|
@@ -166,14 +180,14 @@
|
|
166
180
|
|
167
181
|
トップページのビュー
|
168
182
|
```
|
169
|
-
<% content_for :cover do %>
|
170
183
|
<% if logged_in? %>
|
171
184
|
<div class="row">
|
172
185
|
<aside class="col-md-4">
|
173
186
|
<%= form_for(@post, html: {multipart: true}) do |f| %>
|
174
187
|
<div class="form-group">
|
175
|
-
<%= f.label :picture %>
|
188
|
+
<%= f.label :picture, '写真' %>
|
176
189
|
<%= f.file_field :picture %><br />
|
190
|
+
<%= f.label :content, 'コンテンツ' %>
|
177
191
|
<%= f.text_area :content, class: 'form-control', rows: 5 %>
|
178
192
|
</div>
|
179
193
|
<%= f.submit '投稿', class: 'btn btn-primary btn-md' %>
|
@@ -194,7 +208,6 @@
|
|
194
208
|
</div>
|
195
209
|
</div>
|
196
210
|
<% end %>
|
197
|
-
<% end %>
|
198
211
|
```
|
199
212
|
|
200
213
|
トップページに
|
1
本文の修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
**やりたいこと**
|
2
|
+
トップページにコメント一覧を表示させる
|
3
|
+
|
4
|
+
**問題点**
|
1
5
|
[https://gyazo.com/e07751929a071e6d52390b314ae87ae0](https://gyazo.com/e07751929a071e6d52390b314ae87ae0)の画面で、コメント投稿をしたところ、コメントがトップページに表示されません(コンソール上ではコメントが保存されいています)。
|
2
6
|
|
3
7
|
現在のコードは以下の通りです。
|
@@ -9,13 +13,59 @@
|
|
9
13
|
@user = current_user
|
10
14
|
@post = current_user.posts.build # form_for 用
|
11
15
|
@posts = current_user.feed_posts.order('created_at DESC').page(params[:page])
|
12
|
-
@comment = @post.comments.build
|
13
16
|
@comments = @post.comments.order('created_at DESC').page(params[:page])
|
14
17
|
end
|
15
18
|
end
|
16
19
|
end
|
17
20
|
```
|
18
21
|
|
22
|
+
posts.controller
|
23
|
+
```ここに言語を入力
|
24
|
+
class PostsController < ApplicationController
|
25
|
+
before_action :require_user_logged_in
|
26
|
+
before_action :correct_user, only: [:destroy]
|
27
|
+
|
28
|
+
def index
|
29
|
+
@comment = @post.comments.build
|
30
|
+
end
|
31
|
+
|
32
|
+
def show
|
33
|
+
@post = Post.includes(:user).find(params[:id])
|
34
|
+
end
|
35
|
+
|
36
|
+
def create
|
37
|
+
@post = current_user.posts.build(post_params)
|
38
|
+
if @post.save
|
39
|
+
flash[:success] = 'メッセージを投稿しました。'
|
40
|
+
redirect_to root_url
|
41
|
+
else
|
42
|
+
@posts = current_user.feed_posts.order('created_at DESC').page(params[:page])
|
43
|
+
flash.now[:danger] = 'メッセージの投稿に失敗しました。'
|
44
|
+
render 'toppages/index'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def destroy
|
49
|
+
@post.destroy
|
50
|
+
flash[:success] = 'メッセージを削除しました。'
|
51
|
+
redirect_back(fallback_location: root_path)
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def post_params
|
57
|
+
params.require(:post).permit(:picture, :content)
|
58
|
+
end
|
59
|
+
|
60
|
+
def correct_user
|
61
|
+
@post = current_user.posts.find_by(id: params[:id])
|
62
|
+
unless @post
|
63
|
+
redirect_to root_url
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
```
|
68
|
+
|
19
69
|
comments.controller
|
20
70
|
```
|
21
71
|
class CommentsController < ApplicationController
|
@@ -153,4 +203,6 @@
|
|
153
203
|
@comments = @post.comments.order('created_at DESC').page(params[:page])
|
154
204
|
```
|
155
205
|
を入れれば表示されると思っていましたが、全く表示されませんでした。
|
206
|
+
|
207
|
+
他にも色々試しましたが、それでもコメントは表示されませんでした。
|
156
208
|
これ以上、トップページに投稿したコメントを表示させる方法がわからないので、どなたかご教示をお願いできませんか?
|