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?
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?
Replace
<%= render middle %>
with<%= render middle, :post => post %>
. Then in yourmiddle
partial, you can access thepost
variable.Give it to the partial as a local variable
Of course, rails also has a shortcut for rendering collections:
In this case it will call the partial post for every post with a local variable 'post'
You can even render a spacer template between each post:
Try this:
Like this you'll have a local variable
post
available within the partial.You can now access post as the local variable
post
in the partial