Elliot Elliot -4 years ago 146
Ruby Question

Pass a variable into a partial, rails 3?

I have a loop like such:

<% @posts.each do |post| %>
<% render middle %>
<% end %>

Then in my middle partial, how do I access the current post?

Answer Source

Try this:

<% @posts.each do |post| %>
  <%= render 'middle', :post => post %>
<% end %>

Like this you'll have a local variable post available within the partial.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download