2014-02-07 105 views
0

我有意見屬於屬於問題的答案。我目前正試圖以顯示評論,但我得到一個錯誤:無法訪問雙重嵌套資源?

undefined method `comments' for nil:NilClass 
從該行

顯然 <%=渲染@ 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

你應該創建作爲自己的註釋,它涉及到你的答案:

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

GL HF &。

+0

仍然有一個未定義的方法錯誤,有什麼想法?謝謝! – google1254

+0

什麼是你的params散列?您的控制器中有新的方法嗎? – rlecaro2

+0

DEF創建 \t \t @comment = Comment.create(answer_id:PARAMS [:answer_id]) \t \t \t redirect_to的answer_path(@answer) \t年底 – google1254