如何将对象传递给部分?

时间:2011-09-14 04:25:58

标签: ruby ruby-on-rails-3

这不是你如何将一个对象传递给一个部分:

<% @answers.each do |answer| %>
  ...
  <%= render 'form_for_answer_comment', :answer => answer %> ?
<% end %>

我的部分看起来像:

<%= form_for([@question, @answer, @comment]) do |f| %>

我收到以下错误:

路由错误

No route matches {:action=>"create", :controller=>"questions/answers/comments", :format=>nil, :question_id=>#<Question id: 3, user_id: 1, title: "test", description: "asdfasdfasdfasdfasdfasdf\r\n\r\n\tasdfasd", created_at: "2011-09-10 01:33:38", updated_at: "2011-09-10 01:33:38">, :answer_id=>#<Answer id: nil, user_id: nil, question_id: nil, description: nil, selected: nil, created_at: nil, updated_at: nil>}

但是,它适用于:

<%= form_for([@question, Answer.first, @comment]) do |f| %>

我怀疑问题是它没有正确地将@answer对象传递给部分。我做错了什么?

1 个答案:

答案 0 :(得分:3)

尝试:

<%= render :partial => 'form_for_answer_comment', :locals => { :answer => answer } %>

然后,在您的部分内容中,不使用@进行调用,例如:

<%= form_for([@question, answer, @comment]) do |f| %>