質問編集履歴

8

ファイルパスの追記

2018/02/04 07:39

投稿

begin1990
begin1990

スコア31

test CHANGED
File without changes
test CHANGED
@@ -242,7 +242,7 @@
242
242
 
243
243
 
244
244
 
245
- comments.html.erb
245
+ comments/_comments.html.erb
246
246
 
247
247
  ```
248
248
 
@@ -280,7 +280,7 @@
280
280
 
281
281
 
282
282
 
283
- posts.html.erb
283
+ posts/_posts.html.erb
284
284
 
285
285
  ```
286
286
 

7

本文修正

2018/02/04 07:39

投稿

begin1990
begin1990

スコア31

test CHANGED
File without changes
test CHANGED
@@ -422,9 +422,9 @@
422
422
 
423
423
  ```
424
424
 
425
- @comment = @post.comments.build
425
+ @post_comment = current_user.post_comments.build
426
-
426
+
427
- @comments = @post.comments.order('created_at DESC').page(params[:page])
427
+ @post_comments = @post.post_comments.order('created_at DESC').page(params[:page])
428
428
 
429
429
  ```
430
430
 

6

本文の修正

2018/02/04 07:24

投稿

begin1990
begin1990

スコア31

test CHANGED
File without changes
test CHANGED
@@ -418,7 +418,7 @@
418
418
 
419
419
 
420
420
 
421
- トップページ
421
+ toppages_controller.rb
422
422
 
423
423
  ```
424
424
 

5

テンプレートパスの追記

2018/02/04 07:14

投稿

begin1990
begin1990

スコア31

test CHANGED
File without changes
test CHANGED
@@ -12,7 +12,7 @@
12
12
 
13
13
  現在のコードは以下の通りです。
14
14
 
15
- toppages.controller
15
+ toppages.controller.rb
16
16
 
17
17
  ```
18
18
 
@@ -42,9 +42,11 @@
42
42
 
43
43
 
44
44
 
45
+
46
+
45
- posts.controller
47
+ posts_controller.rb
46
-
48
+
47
- ```ここに言語を入力
49
+ ```
48
50
 
49
51
  class PostsController < ApplicationController
50
52
 
@@ -136,7 +138,7 @@
136
138
 
137
139
 
138
140
 
139
- comments.controller
141
+ post_comments.controller.rb
140
142
 
141
143
  ```
142
144
 
@@ -240,7 +242,7 @@
240
242
 
241
243
 
242
244
 
243
- commentsビュー
245
+ comments.html.erb
244
246
 
245
247
  ```
246
248
 
@@ -278,7 +280,7 @@
278
280
 
279
281
 
280
282
 
281
- postsビュー
283
+ posts.html.erb
282
284
 
283
285
  ```
284
286
 
@@ -352,7 +354,7 @@
352
354
 
353
355
 
354
356
 
355
- トップページのビュー
357
+ toppages/index.html.erb
356
358
 
357
359
  ```
358
360
 

4

コントローラの修正

2018/02/04 07:13

投稿

begin1990
begin1990

スコア31

test CHANGED
File without changes
test CHANGED
@@ -28,6 +28,10 @@
28
28
 
29
29
  @posts = current_user.feed_posts.order('created_at DESC').page(params[:page])
30
30
 
31
+ @post_comment = current_user.post_comments.build
32
+
33
+ @post_comments = @post.post_comments.order('created_at DESC').page(params[:page])
34
+
31
35
  end
32
36
 
33
37
  end
@@ -144,102 +148,92 @@
144
148
 
145
149
 
146
150
 
147
- def index
151
+ def create
148
152
 
149
153
  @post = Post.find(params[:post_id])
150
154
 
155
+ @post_comment = @post.post_comments.create(post_comment_params)
156
+
157
+ @post_comment.user_id = current_user.id
158
+
159
+ #@post_comment = current_user.post_comments.build(post_comment_params)
160
+
161
+ #@comment = Comment.create(text: comment_params[:text], post_id: comment_params[:post_id], user_id: current_user.id)
162
+
163
+ if @post_comment.save
164
+
165
+ flash[:success] = "コメントしました。"
166
+
167
+ #redirect_to "/posts/#{@comment.post.id}"
168
+
169
+ #redirect_to post_comments_path(@post.id)
170
+
171
+ #redirect_to :action =>"new"
172
+
173
+ redirect_to root_url
174
+
175
+ else
176
+
151
- @post_comments = @post.post_comments.order('created_at DESC').page(params[:page])
177
+ @post_comments = @post.post_comments.order('created_at DESC').page(params[:page])
178
+
152
-
179
+ flash.now[:danger] = 'コメントの投稿に失敗しました。'
180
+
181
+ render 'toppages/index'
182
+
153
- end
183
+ end
184
+
154
-
185
+ end
155
-
156
-
186
+
187
+
188
+
157
- def create
189
+ def destroy
190
+
191
+ @post_comment.destroy
192
+
193
+ flash[:success] = 'コメントを削除しました。'
194
+
195
+ redirect_back(fallback_location: root_path)
196
+
197
+ end
198
+
199
+
200
+
201
+ private
202
+
203
+ # Use callbacks to share common setup or constraints between actions.
204
+
205
+ def set_post_comment
158
206
 
159
207
  @post = Post.find(params[:post_id])
160
208
 
161
- @post_comment = @post.post_comments.create(post_comment_params)
209
+ @post_comment = @post.post_comments.find(params[:id])
210
+
162
-
211
+ end
212
+
213
+
214
+
215
+ # Never trust parameters from the scary internet, only allow the white list through.
216
+
217
+ def post_comment_params
218
+
163
- @post_comment.user_id = current_user.id
219
+ params.require(:post_comment).permit(:user_id, :post_id, :content)
220
+
164
-
221
+ end
222
+
223
+
224
+
225
+ def correct_user
226
+
165
- #@post_comment = current_user.post_comments.build(post_comment_params)
227
+ @post_comment = current_user.post_comments.find_by(id: params[:id])
166
-
167
- #@comment = Comment.create(text: comment_params[:text], post_id: comment_params[:post_id], user_id: current_user.id)
228
+
168
-
169
- if @post_comment.save
229
+ unless @post_comment
170
-
171
- flash[:success] = "コメントしました。"
230
+
172
-
173
- #redirect_to "/posts/#{@comment.post.id}"
174
-
175
- #redirect_to post_comments_path(@post.id)
176
-
177
- #redirect_to :action =>"new"
178
-
179
- redirect_to root_url
231
+ redirect_to root_path
180
-
181
- else
182
-
183
- @post_comments = @post.post_comments.order('created_at DESC').page(params[:page])
184
-
185
- flash.now[:danger] = 'コメントの投稿に失敗しました。'
186
-
187
- render 'toppages/index'
188
232
 
189
233
  end
190
234
 
191
235
  end
192
236
 
193
-
194
-
195
- def destroy
196
-
197
- @post_comment.destroy
198
-
199
- flash[:success] = 'コメントを削除しました。'
200
-
201
- redirect_back(fallback_location: root_path)
202
-
203
- end
204
-
205
-
206
-
207
- private
208
-
209
- # Use callbacks to share common setup or constraints between actions.
210
-
211
- def set_post_comment
212
-
213
- @post = Post.find(params[:post_id])
214
-
215
- @post_comment = @post.post_comments.find(params[:id])
216
-
217
- end
218
-
219
-
220
-
221
- # Never trust parameters from the scary internet, only allow the white list through.
222
-
223
- def post_comment_params
224
-
225
- params.require(:post_comment).permit(:user_id, :post_id, :content)
226
-
227
- end
228
-
229
-
230
-
231
- def correct_user
232
-
233
- @post_comment = current_user.post_comments.find_by(id: params[:id])
234
-
235
- unless @post_comment
236
-
237
- redirect_to root_path
238
-
239
- end
240
-
241
- end
242
-
243
237
  end
244
238
 
245
239
  ```

3

本文修正

2018/02/04 04:41

投稿

begin1990
begin1990

スコア31

test CHANGED
File without changes
test CHANGED
@@ -52,7 +52,7 @@
52
52
 
53
53
  def index
54
54
 
55
- @comment = @post.comments.build
55
+ @post_comment = current_user.post_comments.build
56
56
 
57
57
  end
58
58
 
@@ -436,6 +436,8 @@
436
436
 
437
437
 
438
438
 
439
+
440
+
439
441
  他にも色々試しましたが、それでもコメントは表示されませんでした。
440
442
 
441
443
  これ以上、トップページに投稿したコメントを表示させる方法がわからないので、どなたかご教示をお願いできませんか?

2

本文の修正

2018/02/04 04:29

投稿

begin1990
begin1990

スコア31

test CHANGED
File without changes
test CHANGED
@@ -28,8 +28,6 @@
28
28
 
29
29
  @posts = current_user.feed_posts.order('created_at DESC').page(params[:page])
30
30
 
31
- @comments = @post.comments.order('created_at DESC').page(params[:page])
32
-
33
31
  end
34
32
 
35
33
  end
@@ -138,27 +136,37 @@
138
136
 
139
137
  ```
140
138
 
141
- class CommentsController < ApplicationController
139
+ class PostCommentsController < ApplicationController
142
-
143
- #before_action :set_comment, only: [:new,:create, :destroy]
144
140
 
145
141
  before_action :require_user_logged_in
146
142
 
143
+ before_action :correct_user, only: [:destroy]
144
+
145
+
146
+
147
+ def index
148
+
149
+ @post = Post.find(params[:post_id])
150
+
151
+ @post_comments = @post.post_comments.order('created_at DESC').page(params[:page])
152
+
153
+ end
154
+
147
155
 
148
156
 
149
157
  def create
150
158
 
151
159
  @post = Post.find(params[:post_id])
152
160
 
153
- @comment = @post.comments.build(comment_params)
161
+ @post_comment = @post.post_comments.create(post_comment_params)
154
-
162
+
155
- @comment.user_id = current_user.id
163
+ @post_comment.user_id = current_user.id
156
-
164
+
157
- #@comment = current_user.posts.comments.build(comment_params)
165
+ #@post_comment = current_user.post_comments.build(post_comment_params)
158
166
 
159
167
  #@comment = Comment.create(text: comment_params[:text], post_id: comment_params[:post_id], user_id: current_user.id)
160
168
 
161
- if @comment.save
169
+ if @post_comment.save
162
170
 
163
171
  flash[:success] = "コメントしました。"
164
172
 
@@ -172,7 +180,7 @@
172
180
 
173
181
  else
174
182
 
175
- @comments = @post.comments.order('created_at DESC').page(params[:page])
183
+ @post_comments = @post.post_comments.order('created_at DESC').page(params[:page])
176
184
 
177
185
  flash.now[:danger] = 'コメントの投稿に失敗しました。'
178
186
 
@@ -186,7 +194,7 @@
186
194
 
187
195
  def destroy
188
196
 
189
- @comment.destroy
197
+ @post_comment.destroy
190
198
 
191
199
  flash[:success] = 'コメントを削除しました。'
192
200
 
@@ -200,23 +208,37 @@
200
208
 
201
209
  # Use callbacks to share common setup or constraints between actions.
202
210
 
203
- def set_comment
211
+ def set_post_comment
204
-
212
+
205
- @post = Post.find(params[:post_id])
213
+ @post = Post.find(params[:post_id])
206
-
214
+
207
- @comment = @post.comments.find(params[:id])
215
+ @post_comment = @post.post_comments.find(params[:id])
216
+
217
+ end
218
+
219
+
220
+
221
+ # Never trust parameters from the scary internet, only allow the white list through.
222
+
223
+ def post_comment_params
224
+
225
+ params.require(:post_comment).permit(:user_id, :post_id, :content)
226
+
227
+ end
228
+
229
+
230
+
231
+ def correct_user
232
+
233
+ @post_comment = current_user.post_comments.find_by(id: params[:id])
234
+
235
+ unless @post_comment
236
+
237
+ redirect_to root_path
208
238
 
209
239
  end
210
240
 
211
-
212
-
213
- # Never trust parameters from the scary internet, only allow the white list through.
214
-
215
- def comment_params
216
-
217
- params.require(:comment).permit(:user_id, :post_id, :content)
218
-
219
- end
241
+ end
220
242
 
221
243
  end
222
244
 
@@ -230,17 +252,23 @@
230
252
 
231
253
  <ul class="media-list">
232
254
 
233
- <% @comments.each do |comment| %>
255
+ <% @post_comments.each do |post_comment| %>
234
-
235
- <div class="name2">投稿者:<%= link_to comment.user, "/users/#{comment.user_id}" %>&nbsp;&nbsp;投稿日時:<%= comment.created_at.strftime("%Y-%m-%d %H:%M:%S") %></div>
256
+
236
-
237
- <div class="name2"><%= comment.content %></div>
257
+ <% user = post_comment.user %>
238
258
 
239
259
  <div>
240
260
 
261
+ <p>投稿者:<%= link_to user.name, user_path(user) %> <span class="text-muted">commented at <%= post_comment.created_at %></span></p>
262
+
263
+ </div>
264
+
265
+ <p><%= post_comment.content %></p>
266
+
267
+ <div>
268
+
241
- <% if current_user == comment.user %>
269
+ <% if current_user == post_comment.user %>
242
-
270
+
243
- <%= link_to "削除", comment, method: :delete, data: { confirm: "本当に削除してよろしいですか?" }, class: 'btn btn-danger btn-sm' %>
271
+ <%= link_to "削除", post_comment, method: :delete, data: { confirm: "本当に削除してよろしいですか?" }, class: 'btn btn-danger btn-sm' %>
244
272
 
245
273
  <% end %>
246
274
 
@@ -248,7 +276,7 @@
248
276
 
249
277
  <% end %>
250
278
 
251
- <%= paginate comments %>
279
+ <%= paginate @post_comments %>
252
280
 
253
281
  </ul>
254
282
 
@@ -288,7 +316,7 @@
288
316
 
289
317
  <p><%= post.content %></p>
290
318
 
291
- <%= render 'comments/comments', comments: @comments %>
319
+ <%= render 'comments/comments', post_comments: @post_comments %>
292
320
 
293
321
  <br/>
294
322
 
@@ -334,8 +362,6 @@
334
362
 
335
363
  ```
336
364
 
337
- <% content_for :cover do %>
338
-
339
365
  <% if logged_in? %>
340
366
 
341
367
  <div class="row">
@@ -346,10 +372,12 @@
346
372
 
347
373
  <div class="form-group">
348
374
 
349
- <%= f.label :picture %>
375
+ <%= f.label :picture, '写真' %>
350
376
 
351
377
  <%= f.file_field :picture %><br />
352
378
 
379
+ <%= f.label :content, 'コンテンツ' %>
380
+
353
381
  <%= f.text_area :content, class: 'form-control', rows: 5 %>
354
382
 
355
383
  </div>
@@ -390,8 +418,6 @@
390
418
 
391
419
  <% end %>
392
420
 
393
- <% end %>
394
-
395
421
  ```
396
422
 
397
423
 

1

本文の修正

2018/02/04 04:26

投稿

begin1990
begin1990

スコア31

test CHANGED
File without changes
test CHANGED
@@ -1,3 +1,11 @@
1
+ **やりたいこと**
2
+
3
+ トップページにコメント一覧を表示させる
4
+
5
+
6
+
7
+ **問題点**
8
+
1
9
  [https://gyazo.com/e07751929a071e6d52390b314ae87ae0](https://gyazo.com/e07751929a071e6d52390b314ae87ae0)の画面で、コメント投稿をしたところ、コメントがトップページに表示されません(コンソール上ではコメントが保存されいています)。
2
10
 
3
11
 
@@ -20,8 +28,6 @@
20
28
 
21
29
  @posts = current_user.feed_posts.order('created_at DESC').page(params[:page])
22
30
 
23
- @comment = @post.comments.build
24
-
25
31
  @comments = @post.comments.order('created_at DESC').page(params[:page])
26
32
 
27
33
  end
@@ -34,6 +40,100 @@
34
40
 
35
41
 
36
42
 
43
+ posts.controller
44
+
45
+ ```ここに言語を入力
46
+
47
+ class PostsController < ApplicationController
48
+
49
+ before_action :require_user_logged_in
50
+
51
+ before_action :correct_user, only: [:destroy]
52
+
53
+
54
+
55
+ def index
56
+
57
+ @comment = @post.comments.build
58
+
59
+ end
60
+
61
+
62
+
63
+ def show
64
+
65
+ @post = Post.includes(:user).find(params[:id])
66
+
67
+ end
68
+
69
+
70
+
71
+ def create
72
+
73
+ @post = current_user.posts.build(post_params)
74
+
75
+ if @post.save
76
+
77
+ flash[:success] = 'メッセージを投稿しました。'
78
+
79
+ redirect_to root_url
80
+
81
+ else
82
+
83
+ @posts = current_user.feed_posts.order('created_at DESC').page(params[:page])
84
+
85
+ flash.now[:danger] = 'メッセージの投稿に失敗しました。'
86
+
87
+ render 'toppages/index'
88
+
89
+ end
90
+
91
+ end
92
+
93
+
94
+
95
+ def destroy
96
+
97
+ @post.destroy
98
+
99
+ flash[:success] = 'メッセージを削除しました。'
100
+
101
+ redirect_back(fallback_location: root_path)
102
+
103
+ end
104
+
105
+
106
+
107
+ private
108
+
109
+
110
+
111
+ def post_params
112
+
113
+ params.require(:post).permit(:picture, :content)
114
+
115
+ end
116
+
117
+
118
+
119
+ def correct_user
120
+
121
+ @post = current_user.posts.find_by(id: params[:id])
122
+
123
+ unless @post
124
+
125
+ redirect_to root_url
126
+
127
+ end
128
+
129
+ end
130
+
131
+ end
132
+
133
+ ```
134
+
135
+
136
+
37
137
  comments.controller
38
138
 
39
139
  ```
@@ -308,4 +408,8 @@
308
408
 
309
409
  を入れれば表示されると思っていましたが、全く表示されませんでした。
310
410
 
411
+
412
+
413
+ 他にも色々試しましたが、それでもコメントは表示されませんでした。
414
+
311
415
  これ以上、トップページに投稿したコメントを表示させる方法がわからないので、どなたかご教示をお願いできませんか?