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:
<%= affiliation.group %>

and layout _list.html.erb:

  • <%= list_counter %> - <%= list.group %> <%= yield %>
  • And a collection of three affiliations, the output would be:

  • 0 - Some affiliation Some affiliation another affiliation Hello affiliation
  • 1 - another affiliation Some affiliation another affiliation Hello affiliation
  • 2 - Hello affiliation Some affiliation another affiliation Hello affiliation
  • As you can see, instead the first item producing an expected:

  • 0 - Some affiliation Some affiliation
  • the yield gives the output of all collection items.

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