質問編集履歴

1

変更点、内容が全て保存出来ていなかった。

2021/01/19 14:09

投稿

YukiTani
YukiTani

スコア19

test CHANGED
File without changes
test CHANGED
@@ -158,166 +158,52 @@
158
158
 
159
159
  ```rails
160
160
 
161
- Commentコントローラー
162
-
163
-
164
-
165
- class CommentsController < ApplicationController
166
-
167
- def create
161
+ ルーティング
168
-
169
- @comment = Comment.new(comment_params)
162
+
170
-
171
- if @comment.save
163
+
172
-
164
+
173
- redirect_to post_path(@comment.post)
165
+ Rails.application.routes.draw do
174
-
175
- else
166
+
176
-
177
- @post = @comment.post
178
-
179
- @comments = @post.comments
167
+ devise_for :users
180
-
168
+
181
- render "posts/show"
169
+ root to: 'posts#index'
170
+
182
-
171
+ resources :posts, only: [:new, :create, :show, :edit, :update, :destroy] do
172
+
173
+ resources :comments, only: :create
174
+
183
- end
175
+ end
184
-
185
- end
176
+
186
-
187
-
188
-
189
- private
190
-
191
- def comment_params
192
-
193
- params.require(:comment).permit(:text).merge(user_id: current_user.id, post_id: params[:post_id])
177
+ resources :users, only: [:index, :show, :edit, :update, :destroy]
194
-
195
- end
196
178
 
197
179
  end
198
180
 
199
181
  ```
200
182
 
201
-
202
-
203
183
  ```rails
204
184
 
205
- ビューファイル
206
-
207
- <div class="type__comments">
185
+ class Prototype < ApplicationRecord
208
-
209
- <% if user_signed_in? %>
186
+
210
-
211
- <%= form_with model: [@post, @comment], local: true do |f|%>
212
-
213
- <div class="field">
214
-
215
- <%= f.label :text, "コメント" %><br />
216
-
217
- <%= f.text_field :text %>
218
-
219
- </div>
220
-
221
- <div class="actions">
222
-
223
- <%= f.submit "送信する", class: :form__btn %>
224
-
225
- </div>
226
-
227
- <% end %>
228
-
229
- <% end %>
230
-
231
- <ul class="comments_lists">
232
-
233
- <% @comments.each do |comment| %>
234
-
235
- <% if @comments %>
236
-
237
- <li class="comments_list">
238
-
239
- <%= comment.text %>
240
-
241
- <%= link_to comment.user.name, user_path(comment.user_id), class: :comment_user %>
242
-
243
- </li>
244
-
245
- <% end %>
246
-
247
- <% end %>
248
-
249
- </ul>
250
-
251
- </div>
252
-
253
- ```
254
-
255
- ```rails
256
-
257
- ルーティング
258
-
259
-
260
-
261
- Rails.application.routes.draw do
262
-
263
- devise_for :users
187
+ belongs_to :user
264
-
188
+
265
- root to: 'posts#index'
189
+ has_one_attached :image
266
-
190
+
267
- resources :posts, only: [:new, :create, :show, :edit, :update, :destroy] do
191
+ has_many :comments, dependent: :destroy ←ここが抜けていました。 dependent: :destroyも一緒に追記
192
+
193
+
194
+
268
-
195
+ validates :title, presence: true
196
+
197
+ validates :catch_copy, presence: true
198
+
269
- resources :comments, only: :create
199
+ validates :concept, presence: true
270
-
271
- end
200
+
272
-
273
- resources :users, only: [:index, :show, :edit, :update, :destroy]
201
+ validates :image, presence: true
274
202
 
275
203
  end
276
204
 
277
205
  ```
278
206
 
279
- ```rails
280
-
281
- commentモデル
282
-
283
- class Comment < ApplicationRecord
284
-
285
- validates :text, presence: true
286
-
287
- validates :user, presence: true
288
-
289
- validates :text, presence: true
290
-
291
-
292
-
293
- belongs_to :post
294
-
295
- belongs_to :user
296
-
297
- end
298
-
299
-
300
-
301
- postモデル
302
-
303
- class Post < ApplicationRecord
304
-
305
- validates :image, presence: true
306
-
307
-
308
-
309
- belongs_to :user
310
-
311
- has_one_attached :image
312
-
313
-
314
-
315
- end
316
-
317
-
318
-
319
- ```
320
-
321
207
 
322
208
 
323
209
 
@@ -325,17 +211,3 @@
325
211
  ### 試したこと
326
212
 
327
213
  発生しているエラーは「NoMethodError」で、undefined method`comments' というメソッドが定義されていないといことなので、@commentsという変数が定義されていないのでエラーということはわかりましたが、before_action_set_postで @post = Post.find(params[:id])をわたせるようになっています。原因追求に困っております。
328
-
329
-
330
-
331
- どうぞ宜しくお願いします。
332
-
333
-
334
-
335
-
336
-
337
- ### 補足情報(FW/ツールのバージョンなど)
338
-
339
-
340
-
341
- ここにより詳細な情報を記載してください。