Question

Right now I have a rails partial that looks like this:

<%= render :partial => "/talk/partials/comment", :collection => @comments, :locals => {:votes => @votes} %>

I am passing in a collection of comments and another local variable.

That comment partial then goes right into using the comment variable and works fine.

I have since made another partial called '/talk/partials/comment_2014'. When I try this, I am getting the error undefined local variable or method 'comment'. From what I can gather, when I have a different partial name, something with the variable also changes. I would like to keep the same comment variable for the new partial ''/talk/partials/comment_2014'. How would I go about doing this?

Something I tried which did not work was the following:

<% @comments.each do |comment| %>
  <%= render :partial => "/talk/partials/comment_2014", comment: comment, :locals => {:votes => @votes} %>
<% end %>

which did not work either.

Was it helpful?

Solution

You can do it this way

<% @comments.each do |comment| %>
  <%= render "/talk/partials/comment_2014", comment: comment, votes: @votes %>
<% end %>

Or

<% @comments.each do |comment| %>
  <%= render partial: "/talk/partials/comment_2014", locals: { comment: comment, votes: @votes } %>
<% end %>

Notice in the second way the comment is inside the locals.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top