Christian Fazzini
Christian Fazzini

Reputation: 19713

How to pass an object into a partial?

Isn't this how you pass an object to a partial:

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

My partial looks like:

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

I get the following error:

Routing Error

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>}

However, it works with:

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

I suspect the problem is that its not passing the @answer object to the partial correctly. What am I doing wrong?

Upvotes: 0

Views: 444

Answers (1)

sa125
sa125

Reputation: 28971

Try:

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

Then, in your partial, call it without @, e.g:

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

Upvotes: 3

Related Questions