ruby-on-rails: rendering partials on each item in a list

ruby-on-rails

Solution

Since the post variable in the each loop is a locale variable you have to make it available to the partial:

<%= render :partial => "posts/post_show", :locals => {:post => post} %>

You can then access the title through the local variable post:

<td><%=h post.title %> </td>

You could also simplify the whole thing by rendering the posts as collection. Take a look at the Rails documentation for more information:

http://api.rubyonrails.org/classes/ActionController/Base.html#M000658

Problem

I have the following in a view (.html.erb) : ``` <% @posts = GetAllPostsFunctions %> (removed for berivity) <% @posts.each do |post| %> <%= post.title %> <%= render :partial => "posts/post_show" %> <% end %> ``` the posts_show partial has the following: ``` .... <td><%=h @post.title %> </td> ``` But I get the following error ``` You have a nil object when you didn't expect it! The error occurred while evaluating nil.title ``` Any ideas?

Original source