私はRailsを使用して簡単なブログアプリケーションを構築しています。投稿用のコメントを投稿する機能を作成しようとしています。しかし、私はこのエラーを得続けるform_for定義された関連付けを持つRailsの最初の引数nilエラー
私は同様の問題持っていた他の記事をチェックしましたが、見つけることができませんでした
<h3> Submit a Comment </h3>
<%= form_for [@post, @comment] do |c| %>
//Error is here, @comment appears to be nil and I'm not sure why
//(I've checked both cases)
<%= c.label :body, "Comment: " %>
<br />
<%= c.text_area :body %>
nilを含めるか、空にすることはできません形式で
最初の引数溶液。 、解決私のポストコントローラ
みんなありがとうでshowメソッドに@comment = Comment.newを追加するのを忘れ:すべてのヘルプは大
class CommentsController < ApplicationController
def create
@post = Post.find params[:post_id]
comment_params = params.require(:comment).permit(:body)
@comment = Comment.new comment_params
@comment.post = @post
# Why is my comment nil?
if @comment.save
redirect_to post_path(@post), notice: "Comment successful"
else
flash[:alert] = "Comment unsuccessful. Please do not enter an empty comment"
render "/posts/show"
end
end
def destroy
# To be implemented
end
end
EDITをいただければ幸いです!
で、あなたはどのように見えるかをコントローラのでしょうか? – jdgray
は私のコメントのコントローラを追加しました – JKW