我的评论属于问题的答案。我目前正在尝试显示评论,但收到错误:
undefined method `comments' for nil:NilClass
显然是从这条线上来的 <%= render @ answer.comments%>
我的评论表格如下所示
<%= form_for([@answer, @answer.comments.build]) do |f| %>
<p>
<%= f.label :comment %>
<%= f.text_area :comment, :cols => "50", :rows => "30"%>
</p>
<p>
<%= f.submit "Submit Comment" %>
</p>
我的评论控制器看起来像这样
def create
@answer = Answer.find(params[:answer_id])
@comment = @answer.comments.create(params[:comment])
redirect_to answer_path(@answer)
end
评论belongs_to回答并回答has_many评论。谢谢!
答案 0 :(得分:1)
您应该将评论创建为自己并将其与您的答案联系起来:
@comment = Comment.create(answer_id: params[answer_id])
GL&amp; HF。