質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

501閲覧

コメントがDBに保存されず、一覧表示ができません・・・

ire

総合スコア1

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2022/12/26 01:21

前提

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>

よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yuma.inaura

2022/12/26 01:38

@comment.save! にしてエラー内容を見てみてはいかがですか
ire

2022/12/26 02:23

コメントありがとうございます。 @comment.save!にしてみたところ、以下のエラーが出ました。 ActiveRecord::RecordInvalid in CommentsController#create Validation failed: Content can't be blank Extracted source (around line #5): def create @comment = Comment.new(comment_params) if @comment.save! redirect_to prototype_path(@comment.prototype) else @prototype = @comment.prototype Parameters: {"authenticity_token"=>"37/9IJ9GoaMFhPVlcPIb3JiGFSx86GqgHkNflF/YaV5Tg0jhYWnypORskr99VuXHmDVoVRED5vO+Kt9RBFb+LA==", "comment"=>{"comment"=>"すごい"}, "commit"=>"送信する", "prototype_id"=>"1"} これはどうしたら、直るのでしょう・・・。 教えていただけると助かります。 よろしくお願いします。
yuma.inaura

2022/12/26 04:58

content と comment を綴り間違えたりしてませんか
ire

2022/12/26 06:49

ありがとうございます。 無事解決できました! 大変助かりました!
guest

回答1

0

ベストアンサー

content と comment を綴り間違えでしたね

(「質問へのコメント」で解決したので回答として転記)

投稿2022/12/26 07:38

yuma.inaura

総合スコア1453

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問