无法访问双重嵌套资源?

时间:2014-02-07 19:26:31

标签: ruby-on-rails

我的评论属于问题的答案。我目前正在尝试显示评论,但收到错误:

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评论。谢谢!

1 个答案:

答案 0 :(得分:1)

您应该将评论创建为自己并将其与您的答案联系起来:

@comment = Comment.create(answer_id: params[answer_id])

GL&amp; HF。