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

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

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

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

Q&A

解決済

2回答

1258閲覧

log[rails]コメントが表示できない

YutoKubo

総合スコア13

Ruby on Rails 6

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

0グッド

0クリップ

投稿2021/02/11 12:33

編集2021/02/12 02:01

コメントフォームからのコメントが表示されません。
表示する側の問題か、そもそもformから受け取ったオブジェクトがちゃんと保存されていないかのどちらかだと考え、binding.pryを コメント一覧を表示する<% @comments.each do |c| %>の'@comments'にかけてみましたが反応せず、処理がなされていませんでした。しかしそれ以上原因を追求しきれず、お力添えをいただければと思います。エラー文はありません。
<show.html.erb>

,,,,, <% if user_signed_in? %> <%= form_with(model: [@community, @comment], local: true ) do |f| %> <%= f.text_field :content %> <br> <%= f.submit 'コメントする' %> <%end%> <%end%> <h2>コメント一覧</h2> <% @comments.each do |c| %> <%binding.pry%> <%#反応せず%> <div> <%= c.content %> </div> <% end %> </div> ,,,,,

<comments.controller.rb>

class CommentsController < ApplicationController def create @comment = Comment.new(comment_params) @comment.user_id = current_user.id if @comment.save flash[:success] = "コメントしました" redirect_back(fallback_location: root_path) else flash[:success] = "コメントできませんでした" redirect_back(fallback_location: root_path) end end private def comment_params params.require(:comment).permit(:content,:community_id) end end

<communities_controller.rb>

class CommunitiesController < ApplicationController before_action :set_community, only: [:edit,:show,:update] def index @communities = Community.all end def show @comments = @community.comments #投稿詳細に関連付けてあるコメントを全取得 @comment = Comment.new #投稿詳細画面でコメントの投稿を行うので、formのパラメータ用にCommentオブジェクトを取得 end def edit @community = Community.find(params[:id]) end def new @community = Community.new end def create @community = current_user.communities.build(community_params) if @community.save redirect_to community_path(@community), notice: "投稿に成功しました。" else render :new end end def update if @community.update(community_params) redirect_to community_path, notice: "投稿を更新しました。" else render :edit end end def destroy community = Community.find(params[:id]) community.destroy redirect_to communities_path, notice: "投稿を削除しました。" end private def community_params params.require(:community).permit(:title, :body, :image, :content) end def set_community @community = Community.find(params[:id]) end end

<log>(フォームに"こんにちは"と入力)

Started POST "/communities/1/comments" for ::1 at 2021-02-12 10:57:55 +0900 (1.1ms) SELECT sqlite_version(*) Processing by CommentsController#create as HTML Parameters: {"authenticity_token"=>"yViCtrZkddHwZ2kBdEjieduEyXF/FAz0E75iBg4w03R4NYReqB4odspub7p6+pBJ64J0LMSaOIKSJvQNJnboXg==", "comment"=>{"content"=>"こんにちは"}, "commit"=>"コメントする", "community_id"=>"1"} User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]] ↳ app/controllers/comments_controller.rb:4:in `create' (0.1ms) begin transaction ↳ app/controllers/comments_controller.rb:7:in `create' User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] ↳ app/controllers/comments_controller.rb:7:in `create' (0.1ms) rollback transaction ↳ app/controllers/comments_controller.rb:7:in `create' Redirected to http://localhost:3000/communities/1 Completed 302 Found in 38ms (ActiveRecord: 1.1ms | Allocations: 5860) Started GET "/communities/1" for ::1 at 2021-02-12 10:57:55 +0900 Processing by CommunitiesController#show as HTML Parameters: {"id"=>"1"} Community Load (0.9ms) SELECT "communities".* FROM "communities" WHERE "communities"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] ↳ app/controllers/communities_controller.rb:54:in `set_community' Rendering communities/show.html.erb within layouts/application User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]] ↳ app/views/communities/show.html.erb:32 Comment Load (0.6ms) SELECT "comments".* FROM "comments" WHERE "comments"."community_id" = ? [["community_id", 1]] ↳ app/views/communities/show.html.erb:43 Rendered communities/show.html.erb within layouts/application (Duration: 11.1ms | Allocations: 2534) [Webpacker] Everything's up-to-date. Nothing to do Completed 200 OK in 51ms (Views: 43.0ms | ActiveRecord: 1.7ms | Allocations: 12251)

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

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

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

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

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

winterboum

2021/02/11 12:43

communities_controller.rb の def show で使われている  @community はどこで定義していますか
YutoKubo

2021/02/11 13:33

わかりにくくてすいません。communities_controllerrbのコードを全て載せました。before_actionで@community = Community.find(params[:id])をまとめています。
guest

回答2

0

この問題に関して非常に客観的な意見を述べていただきありがとうございます。 mapquest

投稿2023/12/18 02:12

sarausa

総合スコア6

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

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

0

ベストアンサー

@community = Community.find(params[:id]) ですが
@community = Community.find(params[:community_id])みたいな気がする。
ああ、、、だと@comments = @community.commentsでエラーか。。。
logでparamater確認してみてください。

及び
flash[:success] = "コメントしました"
は出ました?

訂正
comment_params に community_id がないですね。
モデルが載ってないので想像ですが belongs_to :community ですよね。
なので、saveに失敗しています。

投稿2021/02/11 14:08

編集2021/02/11 14:13
winterboum

総合スコア23329

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

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

YutoKubo

2021/02/11 14:20

@community = Community.find(params[:community_id])だと Couldn't find Community without an IDとエラーになっちゃいますね。 また、flashメッセージは出ていません...。
winterboum

2021/02/11 14:23

訂正 comment_params に community_id がない の方は試しました?
YutoKubo

2021/02/11 16:12 編集

def comment_params params.require(:comment).permit(:content, :community_id) end こんなかんじでpermitにcommunity_idを追加してみましたが変化なしです。。
winterboum

2021/02/11 22:40

logでParamatersを確認してみてください。 { comment: { content: ***, community_id: *** }} では無いはず。
YutoKubo

2021/02/12 02:03 編集

Parameters: {"authenticity_token"=>"yViCtrZkddHwZ2kBdEjieduEyXF/FAz0E75iBg4w03R4NYReqB4odspub7p6+pBJ64J0LMSaOIKSJvQNJnboXg==", "comment"=>{"content"=>"こんにちは"}, "commit"=>"コメントする", "community_id"=>"1"}  ,,,,,,,,, こうなりますね。
winterboum

2021/02/12 02:37

ですから、@comment = Comment.new に community_id を取り込むには?
YutoKubo

2021/02/15 12:10

[:追記]解説は省きますが、このコードで解決しました @comment = current_user.comments.new(comment_params) @comment.community_id = params[:community_id] if @comment.save!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問