質問編集履歴

9

みやすくするため

2019/04/29 12:46

投稿

dokodoko
dokodoko

スコア20

test CHANGED
File without changes
test CHANGED
@@ -396,7 +396,7 @@
396
396
 
397
397
  def comment_params
398
398
 
399
- params.require(:comment).permit(:content).merge(user_id: current_user.id)
399
+ params.require(:comment).permit(:content)
400
400
 
401
401
  end
402
402
 
@@ -570,7 +570,7 @@
570
570
 
571
571
  def comment_params
572
572
 
573
- params.require(:comment).permit(:content).merge(user_id: current_user.id)
573
+ params.require(:comment).permit(:content)
574
574
 
575
575
  end
576
576
 

8

みやすくするため

2019/04/29 12:46

投稿

dokodoko
dokodoko

スコア20

test CHANGED
File without changes
test CHANGED
@@ -120,9 +120,7 @@
120
120
 
121
121
 
122
122
 
123
-
124
-
125
- おそらく自分の書き方に問題があると思うのですがエラーになってしまいました。
123
+ 自分の書き方に問題があると思うのですがエラーになってしまいました。
126
124
 
127
125
  せっかく回答していただいたのに活かせずに申し訳ありません。
128
126
 

7

みやすくするため

2019/04/29 12:05

投稿

dokodoko
dokodoko

スコア20

test CHANGED
File without changes
test CHANGED
@@ -579,229 +579,3 @@
579
579
  end
580
580
 
581
581
  ```
582
-
583
-
584
-
585
- _____________________________________________________________________________
586
-
587
- ご回答ありがとうございます。
588
-
589
- 修正してみました。
590
-
591
-
592
-
593
-
594
-
595
-
596
-
597
-
598
-
599
-
600
-
601
-
602
-
603
-
604
-
605
-
606
-
607
-
608
-
609
- ```
610
-
611
- class CommentsController < ApplicationController
612
-
613
-
614
-
615
- def create
616
-
617
- @comment = Comment.new(comment_params)
618
-
619
- @comment.user_id = current_user.id
620
-
621
- if @comment.save
622
-
623
- redirect_back(fallback_location: root_path)
624
-
625
- else
626
-
627
- redirect_back(fallback_location: root_path)
628
-
629
- end
630
-
631
- end
632
-
633
-
634
-
635
- private
636
-
637
- def comment_params
638
-
639
- params.require(:comment).permit(:content)
640
-
641
- end
642
-
643
- end
644
-
645
- ```
646
-
647
-
648
-
649
-
650
-
651
-
652
-
653
-
654
-
655
- ```
656
-
657
- <h1>投稿詳細ページ</h1>
658
-
659
- <h3><%= @post.user.email %></h3>
660
-
661
- <h3><%= @post.content %></h3>
662
-
663
-
664
-
665
- <h2>コメント一覧</h2>
666
-
667
- <% @comments.each do |c| %>
668
-
669
- <div>
670
-
671
- <a href="/users/<%= @post.user.id %>"><%= c.user.email %></a>
672
-
673
- <%= c.content %>
674
-
675
- <hr>
676
-
677
- </div>
678
-
679
- <% end %>
680
-
681
-
682
-
683
- <%= form_for @comment do |f| %>
684
-
685
- <%= f.text_field :content %>
686
-
687
- <br>
688
-
689
- <%= f.submit 'コメントする' %>
690
-
691
- <% end %>
692
-
693
-
694
-
695
- <%= link_to "ホームへ戻る", posts_path %>
696
-
697
-
698
-
699
- ```
700
-
701
-
702
-
703
-
704
-
705
-
706
-
707
- エラーがでました。
708
-
709
- ```
710
-
711
- NoMethodError in Posts#show
712
-
713
- app/views/posts/show.html.erb where line #14 raised:
714
-
715
- undefined method `comments_path' for
716
-
717
- Did you mean? font_path
718
-
719
-
720
-
721
- Extracted source (around line #14):
722
-
723
- <% end %>
724
-
725
-
726
-
727
- <%= form_for @comment do |f| %>
728
-
729
- <%= f.text_field :content %>
730
-
731
- <br>
732
-
733
- <%= f.submit 'コメントする' %>
734
-
735
-
736
-
737
-
738
-
739
- ```
740
-
741
-
742
-
743
-
744
-
745
-
746
-
747
-
748
-
749
- なのでroutesを修正しました。
750
-
751
-
752
-
753
- ```
754
-
755
- Rails.application.routes.draw do
756
-
757
- devise_for :users
758
-
759
- resources :users, only: [:index, :show]
760
-
761
- resources :posts, only: [:index, :show, :create] do
762
-
763
- resources :comments, only: [:create]
764
-
765
- end
766
-
767
-
768
-
769
- root 'posts#index'
770
-
771
- end
772
-
773
- ```
774
-
775
-
776
-
777
-
778
-
779
-
780
-
781
-
782
-
783
- ```
784
-
785
- Rails.application.routes.draw do
786
-
787
- devise_for :users
788
-
789
- resources :users, only: [:index, :show]
790
-
791
- resources :posts, only: [:index, :show, :create]
792
-
793
- resources :comments
794
-
795
-
796
-
797
- root 'posts#index'
798
-
799
- end
800
-
801
- ```
802
-
803
-
804
-
805
- ネストをはずしたらエラーは解決してコメント出来るようになったのですがやはりデータベースにデータが保存されません。
806
-
807
- 自分の記述に何かしらのミスがあり解決出来ませんでした。せっかくご回答して頂いてるのに申し訳ありません。

6

読みやすくするため

2019/04/29 12:02

投稿

dokodoko
dokodoko

スコア20

test CHANGED
File without changes
test CHANGED
@@ -582,16 +582,226 @@
582
582
 
583
583
 
584
584
 
585
-
586
-
587
-
588
-
589
-
590
-
591
-
592
-
593
-
594
-
595
-
596
-
597
- どなたかお助けしてくださると助かります。
585
+ _____________________________________________________________________________
586
+
587
+ ご回答ありがとうございます。
588
+
589
+ 修正してみました。
590
+
591
+
592
+
593
+
594
+
595
+
596
+
597
+
598
+
599
+
600
+
601
+
602
+
603
+
604
+
605
+
606
+
607
+
608
+
609
+ ```
610
+
611
+ class CommentsController < ApplicationController
612
+
613
+
614
+
615
+ def create
616
+
617
+ @comment = Comment.new(comment_params)
618
+
619
+ @comment.user_id = current_user.id
620
+
621
+ if @comment.save
622
+
623
+ redirect_back(fallback_location: root_path)
624
+
625
+ else
626
+
627
+ redirect_back(fallback_location: root_path)
628
+
629
+ end
630
+
631
+ end
632
+
633
+
634
+
635
+ private
636
+
637
+ def comment_params
638
+
639
+ params.require(:comment).permit(:content)
640
+
641
+ end
642
+
643
+ end
644
+
645
+ ```
646
+
647
+
648
+
649
+
650
+
651
+
652
+
653
+
654
+
655
+ ```
656
+
657
+ <h1>投稿詳細ページ</h1>
658
+
659
+ <h3><%= @post.user.email %></h3>
660
+
661
+ <h3><%= @post.content %></h3>
662
+
663
+
664
+
665
+ <h2>コメント一覧</h2>
666
+
667
+ <% @comments.each do |c| %>
668
+
669
+ <div>
670
+
671
+ <a href="/users/<%= @post.user.id %>"><%= c.user.email %></a>
672
+
673
+ <%= c.content %>
674
+
675
+ <hr>
676
+
677
+ </div>
678
+
679
+ <% end %>
680
+
681
+
682
+
683
+ <%= form_for @comment do |f| %>
684
+
685
+ <%= f.text_field :content %>
686
+
687
+ <br>
688
+
689
+ <%= f.submit 'コメントする' %>
690
+
691
+ <% end %>
692
+
693
+
694
+
695
+ <%= link_to "ホームへ戻る", posts_path %>
696
+
697
+
698
+
699
+ ```
700
+
701
+
702
+
703
+
704
+
705
+
706
+
707
+ エラーがでました。
708
+
709
+ ```
710
+
711
+ NoMethodError in Posts#show
712
+
713
+ app/views/posts/show.html.erb where line #14 raised:
714
+
715
+ undefined method `comments_path' for
716
+
717
+ Did you mean? font_path
718
+
719
+
720
+
721
+ Extracted source (around line #14):
722
+
723
+ <% end %>
724
+
725
+
726
+
727
+ <%= form_for @comment do |f| %>
728
+
729
+ <%= f.text_field :content %>
730
+
731
+ <br>
732
+
733
+ <%= f.submit 'コメントする' %>
734
+
735
+
736
+
737
+
738
+
739
+ ```
740
+
741
+
742
+
743
+
744
+
745
+
746
+
747
+
748
+
749
+ なのでroutesを修正しました。
750
+
751
+
752
+
753
+ ```
754
+
755
+ Rails.application.routes.draw do
756
+
757
+ devise_for :users
758
+
759
+ resources :users, only: [:index, :show]
760
+
761
+ resources :posts, only: [:index, :show, :create] do
762
+
763
+ resources :comments, only: [:create]
764
+
765
+ end
766
+
767
+
768
+
769
+ root 'posts#index'
770
+
771
+ end
772
+
773
+ ```
774
+
775
+
776
+
777
+
778
+
779
+
780
+
781
+
782
+
783
+ ```
784
+
785
+ Rails.application.routes.draw do
786
+
787
+ devise_for :users
788
+
789
+ resources :users, only: [:index, :show]
790
+
791
+ resources :posts, only: [:index, :show, :create]
792
+
793
+ resources :comments
794
+
795
+
796
+
797
+ root 'posts#index'
798
+
799
+ end
800
+
801
+ ```
802
+
803
+
804
+
805
+ ネストをはずしたらエラーは解決してコメント出来るようになったのですがやはりデータベースにデータが保存されません。
806
+
807
+ 自分の記述に何かしらのミスがあり解決出来ませんでした。せっかくご回答して頂いてるのに申し訳ありません。

5

自分なりに試したみたのですが改善できませんでした。

2019/04/29 11:01

投稿

dokodoko
dokodoko

スコア20

test CHANGED
File without changes
test CHANGED
@@ -261,3 +261,337 @@
261
261
 
262
262
 
263
263
  <%= link_to "ホームへ戻る", posts_path %>
264
+
265
+
266
+
267
+
268
+
269
+
270
+
271
+
272
+
273
+
274
+
275
+
276
+
277
+
278
+
279
+
280
+
281
+
282
+
283
+
284
+
285
+ _______________________________________________________________
286
+
287
+ 改善点
288
+
289
+
290
+
291
+
292
+
293
+
294
+
295
+
296
+
297
+ ```
298
+
299
+ class CommentsController < ApplicationController
300
+
301
+
302
+
303
+ def create
304
+
305
+ @comment = post.comments.build(current_user)
306
+
307
+ if @comment.save
308
+
309
+ redirect_back(fallback_location: root_path)
310
+
311
+ else
312
+
313
+ redirect_back(fallback_location: root_path)
314
+
315
+ end
316
+
317
+
318
+
319
+ end
320
+
321
+
322
+
323
+ private
324
+
325
+ def comment_params
326
+
327
+ params.require(:comment).permit(:content)
328
+
329
+ end
330
+
331
+ end
332
+
333
+ ```
334
+
335
+
336
+
337
+
338
+
339
+ NameError in CommentsController#create
340
+
341
+ undefined local variable or method `post' for #
342
+
343
+
344
+
345
+ Extracted source (around line #4):
346
+
347
+
348
+
349
+ def create
350
+
351
+ @comment = post.comments.build(current_user)
352
+
353
+ if @comment.save
354
+
355
+ redirect_back(fallback_location: root_path)
356
+
357
+ else
358
+
359
+
360
+
361
+
362
+
363
+ エラーをみるにおそらく@comment = post.comments.build(current_user)
364
+
365
+ 上の文にあるこのpostが定義されていないという事だと思います。
366
+
367
+
368
+
369
+ 試した事
370
+
371
+ ```
372
+
373
+ class CommentsController < ApplicationController
374
+
375
+
376
+
377
+ def create
378
+
379
+ @comment = @post.comments.build(comment_params)
380
+
381
+ if @comment.save
382
+
383
+ redirect_back(fallback_location: root_path)
384
+
385
+ else
386
+
387
+ redirect_back(fallback_location: root_path)
388
+
389
+ end
390
+
391
+
392
+
393
+ end
394
+
395
+
396
+
397
+ private
398
+
399
+ def comment_params
400
+
401
+ params.require(:comment).permit(:content).merge(user_id: current_user.id)
402
+
403
+ end
404
+
405
+ end
406
+
407
+
408
+
409
+ ```
410
+
411
+
412
+
413
+
414
+
415
+ エラーの原因としてform_forへ受け渡しができていないとおもいましたが違いました。
416
+
417
+
418
+
419
+ <%= form_for [@post,@comment] do |f| %>
420
+
421
+ <%= f.text_field :content %>
422
+
423
+ <br>
424
+
425
+ <%= f.submit 'コメントする' %>
426
+
427
+ <% end %>
428
+
429
+
430
+
431
+ <%= link_to "ホームへ戻る", posts_path %>
432
+
433
+
434
+
435
+
436
+
437
+
438
+
439
+
440
+
441
+
442
+
443
+
444
+
445
+
446
+
447
+
448
+
449
+ _______________________________________________________________
450
+
451
+ 改善点②
452
+
453
+
454
+
455
+
456
+
457
+ @comment = current_user.comment.build(comment_params)
458
+
459
+ 上を下に改善した結果エラーは改善しました。ですがやはりデータベースに保存されません。
460
+
461
+ @comment = current_user.comments.build(comment_params)
462
+
463
+
464
+
465
+ ```
466
+
467
+ def create
468
+
469
+ @comment = current_user.comments.build(comment_params)
470
+
471
+ if @comment.save
472
+
473
+ redirect_back(fallback_location: root_path)
474
+
475
+ else
476
+
477
+ redirect_back(fallback_location: root_path)
478
+
479
+ end
480
+
481
+
482
+
483
+ end
484
+
485
+
486
+
487
+ private
488
+
489
+ def comment_params
490
+
491
+ params.require(:comment).permit(:content)
492
+
493
+ end
494
+
495
+ end
496
+
497
+ ```
498
+
499
+
500
+
501
+
502
+
503
+
504
+
505
+ _______________________________________________________________
506
+
507
+ 次に
508
+
509
+
510
+
511
+
512
+
513
+
514
+
515
+ binding.pryで確認しました。 
516
+
517
+
518
+
519
+ ```
520
+
521
+ "utf8"=>"✓", "authenticity_token"=>
522
+
523
+ "comment"=>{"content"=>"v"}, "commit"=>"コメントする", "controller"=>"comments", "action"=>"create", "post_id"=>"4"} permitted: false>
524
+
525
+
526
+
527
+ ```
528
+
529
+
530
+
531
+ 上記を見るにデータ自体はおくられています。ですがここをみるに → permitted: false
532
+
533
+
534
+
535
+ ここで許可が降りてないのだと思います。
536
+
537
+
538
+
539
+
540
+
541
+ ```
542
+
543
+ class CommentsController < ApplicationController
544
+
545
+
546
+
547
+ def create
548
+
549
+
550
+
551
+ binding.pry
552
+
553
+ @comment = post.comments.build(comment_params)
554
+
555
+ if @comment.save
556
+
557
+ redirect_back(fallback_location: root_path)
558
+
559
+ else
560
+
561
+ redirect_back(fallback_location: root_path)
562
+
563
+ end
564
+
565
+
566
+
567
+ end
568
+
569
+
570
+
571
+ private
572
+
573
+ def comment_params
574
+
575
+ params.require(:comment).permit(:content).merge(user_id: current_user.id)
576
+
577
+ end
578
+
579
+ end
580
+
581
+ ```
582
+
583
+
584
+
585
+
586
+
587
+
588
+
589
+
590
+
591
+
592
+
593
+
594
+
595
+
596
+
597
+ どなたかお助けしてくださると助かります。

4

みやすくするため

2019/04/20 06:05

投稿

dokodoko
dokodoko

スコア20

test CHANGED
File without changes
test CHANGED
@@ -122,9 +122,9 @@
122
122
 
123
123
 
124
124
 
125
-
125
+ おそらく自分の書き方に問題があると思うのですがエラーになってしまいました。
126
-
126
+
127
- 回答ありがとうござい
127
+ せっかく回答していただいたのに活かせずに申し訳ありません
128
128
 
129
129
 
130
130
 
@@ -140,12 +140,6 @@
140
140
 
141
141
 
142
142
 
143
- ```class CommentsController < ApplicationController
144
-
145
-
146
-
147
-
148
-
149
143
 
150
144
 
151
145
  def create
@@ -178,7 +172,15 @@
178
172
 
179
173
  end
180
174
 
175
+
176
+
181
- ```
177
+ ```
178
+
179
+
180
+
181
+
182
+
183
+
182
184
 
183
185
 
184
186
 

3

書き間違いがありましたので変更します

2019/04/16 03:20

投稿

dokodoko
dokodoko

スコア20

test CHANGED
File without changes
test CHANGED
@@ -242,7 +242,11 @@
242
242
 
243
243
 
244
244
 
245
+
246
+
247
+
248
+
245
- <%= form_for [@post, @comment] do |f| %>
249
+ <%= form_for [@comment] do |f| %>   ※@post を消す
246
250
 
247
251
  <%= f.text_field :content %>
248
252
 

2

自分なりに試したみたのですが改善できませんでした。せっかく回答してくださったのに申し訳ありません。

2019/04/16 03:16

投稿

dokodoko
dokodoko

スコア20

test CHANGED
File without changes
test CHANGED
@@ -113,3 +113,145 @@
113
113
  教えてもらえると助かりますm(_ _)m
114
114
 
115
115
  よろしくお願いします
116
+
117
+
118
+
119
+
120
+
121
+
122
+
123
+
124
+
125
+
126
+
127
+ 回答ありがとうございます。
128
+
129
+
130
+
131
+ 変更した記述
132
+
133
+
134
+
135
+ ```
136
+
137
+ class CommentsController < ApplicationController
138
+
139
+
140
+
141
+
142
+
143
+ ```class CommentsController < ApplicationController
144
+
145
+
146
+
147
+
148
+
149
+
150
+
151
+ def create
152
+
153
+ @comment = current_user.comment.build(comment_params)
154
+
155
+ if @comment.save
156
+
157
+ redirect_back(fallback_location: root_path)
158
+
159
+ else
160
+
161
+ redirect_back(fallback_location: root_path)
162
+
163
+ end
164
+
165
+
166
+
167
+ end
168
+
169
+
170
+
171
+ private
172
+
173
+ def comment_params
174
+
175
+ params.require(:comment).permit(:content)
176
+
177
+ end
178
+
179
+ end
180
+
181
+ ```
182
+
183
+
184
+
185
+ 出てしまったエラー
186
+
187
+ NoMethodError in CommentsController#create
188
+
189
+ undefined method `comment' for #<User
190
+
191
+ Did you mean? comments comments=
192
+
193
+ Extracted source (around line #3)
194
+
195
+ class CommentsController < ApplicationController
196
+
197
+ def create
198
+
199
+ @comment = current_user.comment.build(comment_params)
200
+
201
+ @comment.user_id = current_user.id
202
+
203
+ if @comment.save
204
+
205
+ redirect_back(fallback_location: root_path)
206
+
207
+
208
+
209
+
210
+
211
+
212
+
213
+ 試したこと
214
+
215
+
216
+
217
+ こちらの記述を変更する
218
+
219
+ <h1>投稿詳細ページ</h1>
220
+
221
+ <h3><%= @post.user.email %></h3>
222
+
223
+ <h3><%= @post.content %></h3>
224
+
225
+
226
+
227
+ <h2>コメント一覧</h2>
228
+
229
+ <% @comments.each do |c| %>
230
+
231
+ <div>
232
+
233
+ <a href="/users/<%= @post.user.id %>"><%= c.user.email %></a>
234
+
235
+ <%= c.content %>
236
+
237
+ <hr>
238
+
239
+ </div>
240
+
241
+ <% end %>
242
+
243
+
244
+
245
+ <%= form_for [@post, @comment] do |f| %>
246
+
247
+ <%= f.text_field :content %>
248
+
249
+ <br>
250
+
251
+ <%= f.submit 'コメントする' %>
252
+
253
+ <% end %>
254
+
255
+
256
+
257
+ <%= link_to "ホームへ戻る", posts_path %>

1

タイトルがわかりにくかったので編集させて頂きました。

2019/04/16 03:14

投稿

dokodoko
dokodoko

スコア20

test CHANGED
@@ -1 +1 @@
1
- データベースにデータが保存されない。
1
+ railsでcreateメソッドをしてもデータベースにデータが保存されない。
test CHANGED
File without changes