https://qiita.com/nojinoji/items/2034764897c6e91ef982
のサイトで練習しています。
エラーはでないのですがhttps://gyazo.com/f1e8856d95901e3f36973425b4bb7517
のようにコメントをcreateしても、なぜかデータベースに保存されません。
class CommentsController < ApplicationController def create @comment = Comment.new(comment_params) @comment.user_id = current_user.id if @comment.save redirect_back(fallback_location: root_path) else redirect_back(fallback_location: root_path) end end private def comment_params params.require(:comment).permit(:content) end end
<h1>投稿詳細ページ</h1> <h3><%= @post.user.email %></h3> <h3><%= @post.content %></h3> <h2>コメント一覧</h2> <% @comments.each do |c| %> <div> <a href="/users/<%= @post.user.id %>"><%= c.user.email %></a> <%= c.content %> <hr> </div> <% end %> <%= form_for [@post, @comment] do |f| %> <%= f.text_field :content %> <br> <%= f.submit 'コメントする' %> <% end %> <%= link_to "ホームへ戻る", posts_path %>
教えてもらえると助かりますm(_ _)m
よろしくお願いします
自分の書き方に問題があると思うのですがエラーになってしまいました。
せっかく回答していただいたのに活かせずに申し訳ありません。
変更した記述
class CommentsController < ApplicationController def create @comment = current_user.comment.build(comment_params) if @comment.save redirect_back(fallback_location: root_path) else redirect_back(fallback_location: root_path) end end private def comment_params params.require(:comment).permit(:content) end end
出てしまったエラー
NoMethodError in CommentsController#create
undefined method `comment' for #<User
Did you mean? comments comments=
Extracted source (around line #3)
class CommentsController < ApplicationController
def create
@comment = current_user.comment.build(comment_params)
@comment.user_id = current_user.id
if @comment.save
redirect_back(fallback_location: root_path)
試したこと
こちらの記述を変更する
<h1>投稿詳細ページ</h1> <h3><%= @post.user.email %></h3> <h3><%= @post.content %></h3> <h2>コメント一覧</h2> <% @comments.each do |c| %> <div> <a href="/users/<%= @post.user.id %>"><%= c.user.email %></a> <%= c.content %> <hr> </div> <% end %><%= form_for [@comment] do |f| %> ※@post を消す
<%= f.text_field :content %>
<br>
<%= f.submit 'コメントする' %>
<% end %>
<%= link_to "ホームへ戻る", posts_path %>
改善点
①
class CommentsController < ApplicationController def create @comment = post.comments.build(current_user) if @comment.save redirect_back(fallback_location: root_path) else redirect_back(fallback_location: root_path) end end private def comment_params params.require(:comment).permit(:content) end end
NameError in CommentsController#create
undefined local variable or method `post' for #
Extracted source (around line #4):
def create
@comment = post.comments.build(current_user)
if @comment.save
redirect_back(fallback_location: root_path)
else
エラーをみるにおそらく@comment = post.comments.build(current_user)
上の文にあるこのpostが定義されていないという事だと思います。
試した事
class CommentsController < ApplicationController def create @comment = @post.comments.build(comment_params) if @comment.save redirect_back(fallback_location: root_path) else redirect_back(fallback_location: root_path) end end private def comment_params params.require(:comment).permit(:content) end end
エラーの原因としてform_forへ受け渡しができていないとおもいましたが違いました。
<%= form_for [@post,@comment] do |f| %>
<%= f.text_field :content %>
<br>
<%= f.submit 'コメントする' %>
<% end %>
<%= link_to "ホームへ戻る", posts_path %>
改善点②
@comment = current_user.comment.build(comment_params)
上を下に改善した結果エラーは改善しました。ですがやはりデータベースに保存されません。
@comment = current_user.comments.build(comment_params)
def create @comment = current_user.comments.build(comment_params) if @comment.save redirect_back(fallback_location: root_path) else redirect_back(fallback_location: root_path) end end private def comment_params params.require(:comment).permit(:content) end end
次に
binding.pryで確認しました。
"utf8"=>"✓", "authenticity_token"=> "comment"=>{"content"=>"v"}, "commit"=>"コメントする", "controller"=>"comments", "action"=>"create", "post_id"=>"4"} permitted: false>
上記を見るにデータ自体はおくられています。ですがここをみるに → permitted: false
ここで許可が降りてないのだと思います。
class CommentsController < ApplicationController def create binding.pry @comment = post.comments.build(comment_params) if @comment.save redirect_back(fallback_location: root_path) else redirect_back(fallback_location: root_path) end end private def comment_params params.require(:comment).permit(:content) end end
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/04/29 13:15
2019/04/29 13:34
2019/04/29 13:43
2019/04/29 15:28
2019/04/29 19:09 編集