前提
ruby on railsでprotospaceというアプリを作成中です。
コメント一覧を表示する機能を実装中です。
エラー文は出ませんが、コメントが表示されません。
またDBにも保存されていません。
プログラミング全くの初心者のため、教えていただけると大変助かります。
実現したいこと
コメントをDBに保存し、一覧を表示したいです。
該当のソースコード
routes.rb
ruby
1Rails.application.routes.draw do 2 devise_for :users 3 root to: "prototypes#index" 4 resources :prototypes do 5 resources :comments, only: :create 6 end 7end 8
prototypes_controller.rb
ruby
1class PrototypesController < ApplicationController 2 3 def index 4 @prototypes = Prototype.all.includes(:user) 5 end 6 7 def new 8 @prototype = Prototype.new 9 end 10 11 def create 12 @prototype = Prototype.new(prototype_params) 13 if @prototype.save 14 redirect_to root_path 15 else 16 render :new 17 end 18 end 19 20 def show 21 @prototype = Prototype.find(params[:id]) 22 @comment = Comment.new 23 @comments = @prototype.comments.includes(:user) 24 end 25 26 def edit 27 @prototype = Prototype.find(params[:id]) 28 end 29 30 def update 31 @prototype = Prototype.find(params[:id]) 32 if @prototype.update(prototype_params) 33 redirect_to prototype_path(@prototype.id) 34 else 35 render :edit 36 end 37 end 38 39 def destroy 40 prototype = Prototype.find(params[:id]) 41 prototype.destroy 42 redirect_to root_path 43 end 44 45 private 46 47 def prototype_params 48 params.require(:prototype).permit(:title, :catch_copy, :concept, :image).merge(user_id: current_user.id) 49 end 50end 51 52
comments_controller.rb
ruby
1class CommentsController < ApplicationController 2 3 def create 4 binding.pry 5 @comment = Comment.new(comment_params) 6 if @comment.save 7 redirect_to prototype_path(@comment.prototype) 8 else 9 @prototype = @comment.prototype 10 @comments = @prototype.comments.includes(:user) 11 render "prototypes/show" 12 end 13 end 14 15 private 16 def comment_params 17 params.require(:comment).permit(:content).merge(user_id: current_user.id, prototype_id: params[:prototype_id]) 18 end 19 20end 21
prototypes > show.html.erb
ruby
1<main class="main"> 2 <div class="inner"> 3 <div class="prototype__wrapper"> 4 <p class="prototype__hedding"> 5 <%= @prototype.title %> 6 </p> 7 <%= link_to @prototype.user.name, root_path, class: :prototype__user %> 8 <%# プロトタイプの投稿者とログインしているユーザーが同じであれば以下を表示する %> 9 <% if user_signed_in? && current_user.id == @prototype.user_id %> 10 <div class="prototype__manage"> 11 <%= link_to "編集する", edit_prototype_path(@prototype), class: :prototype__btn %> 12 <%= link_to "削除する", prototype_path(@prototype), method: :delete, class: :prototype__btn %> 13 </div> 14 <% end %> 15 <%# // プロトタイプの投稿者とログインしているユーザーが同じであれば上記を表示する %> 16 <div class="prototype__image"> 17 <%= image_tag @prototype.image %> 18 </div> 19 <div class="prototype__body"> 20 <div class="prototype__detail"> 21 <p class="detail__title">キャッチコピー</p> 22 <p class="detail__message"> 23 <%= @prototype.catch_copy %> 24 </p> 25 </div> 26 <div class="prototype__detail"> 27 <p class="detail__title">コンセプト</p> 28 <p class="detail__message"> 29 <%= @prototype.concept %> 30 </p> 31 </div> 32 </div> 33 <div class="prototype__comments"> 34 <%# ログインしているユーザーには以下のコメント投稿フォームを表示する %> 35 <% if user_signed_in? %> 36 <%= form_with model: [@prototype, @comment],local: true do |f|%> 37 <div class="field"> 38 <%= f.label :comment, "コメント" %><br /> 39 <%= f.text_field :comment, id:"comment_content" %> 40 </div> 41 <div class="actions"> 42 <%= f.submit "送信する", class: :form__btn %> 43 </div> 44 <% end %> 45 <% end %> 46 <%# // ログインしているユーザーには上記を表示する %> 47 <ul class="comments_lists"> 48 <%# 投稿に紐づくコメントを一覧する処理を記述する %> 49 <li class="comments_list"> 50 <% @comments.each do |comment| %> 51 <li class="comments_list"> 52 <%= comment.content %> 53 </li> 54 <%= link_to comment.user.name, root_path, class: :comment_user %> 55 <% end %> 56 </li> 57 <%# // 投稿に紐づくコメントを一覧する処理を記述する %> 58 </ul> 59 </div> 60 </div> 61 </div> 62</main> 63 64
試したこと
comment_controllerのcreateアクションで
binding.pryをし、paramsの中身を確認したところ、ターミナルで
以下のように出ました。
3: def create
=> 4: binding.pry
5: @comment = Comment.new(comment_params)
6: if @comment.save
7: redirect_to prototype_path(@comment.prototype)
8: else
9: @prototype = @comment.prototype
10: @comments = @prototype.comments.includes(:user)
11: render "prototypes/show"
12: end
13: end
[1] pry(#<CommentsController>)> params
=> <ActionController::Parameters {"authenticity_token"=>"p7qFQmQLJbHhIoO6XXRR+TxnZdeDUfS4H8USw+0khiCcqRbDYyaxVyk7XXSCpreZAgZPa9kVM7IdsY09btO8jA==", "comment"=>{"comment"=>"すごい"}, "commit"=>"送信する", "controller"=>"comments", "action"=>"create", "prototype_id"=>"1"} permitted: false>
[2] pry(#<CommentsController>)> comment_params
Unpermitted parameter: :comment
User Load (32.9ms) SELECT users
.* FROM users
WHERE users
.id
= 1 ORDER BY users
.id
ASC LIMIT 1
↳ app/controllers/comments_controller.rb:17:in `comment_params'
=> <ActionController::Parameters {"user_id"=>1, "prototype_id"=>"1"} permitted: true>
よろしくお願いします。

回答1件
あなたの回答
tips
プレビュー