Rendering a partial collection with a layout

First I tried

<%= render :partial => "affiliations/affiliation", :layout => 'affiliations/list', :collection => @affiliations %>

But the layout doesn't receive the `affiliation` instance variable as the partial does, so it throws a NoMethodError.

Then I tried (I'm running edge) <%= render :partial => "affiliations/affiliation", :layout => 'affiliations/list', :collection => @affiliations, :as => 'affiliation' %>

and it doesn't throw NoMethodError anymore, but the instance variable is nil, which causes other errors.

I don't think I've seen this issue brought up before, but I want to make sure I'm not missing something before I start digging in the code base. Has anyone else been bit by this?

Okay, so it seems the local partial variables, affiliation and affiliation_counter, are actually named list and list_counter in the layout, which makes sense since it's using the name of the layout file - it's probably more useful that way, actually.

So, no errors anymore, but the catch is the yield for each collection item produces the concatenated output of the whole collection!

So

with partial _affiliation.html.erb: <b><%= affiliation.group %></b>

and layout _list.html.erb: <li><%= list_counter %> - <%= list.group %> <%= yield %> </li>

And a collection of three affiliations, the output would be:

<li>0 - Some affiliation <b>Some affiliation</b> <b>another affiliation</b> <b>Hello affiliation</b> </li> <li>1 - another affiliation <b>Some affiliation</b> <b>another affiliation</b> <b>Hello affiliation</b> </li> <li>2 - Hello affiliation <b>Some affiliation</b> <b>another affiliation</b> <b>Hello affiliation</b> </li>

As you can see, instead the first item producing an expected: <li>0 - Some affiliation <b>Some affiliation</b> </li>

the yield gives the output of all collection items.

Something gives me the feeling layouts weren't intended for collections. heh.