コメントフォームからのコメントが表示されません。
表示する側の問題か、そもそも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)
回答1件
あなたの回答
tips
プレビュー