# Rendering a partial collection with a layout

**URL:** <https://www.ruby-forum.com/t/rendering-a-partial-collection-with-a-layout/145003>\
**Category:** Rails\
**Created:** [August 5, 2008, 2:47am UTC](https://www.ruby-forum.com/t/rendering-a-partial-collection-with-a-layout/145003 "2008-08-05T02:47:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![zach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/51bf81/32.png) [@zach](https://www.ruby-forum.com/u/zach)\
**Post date:** [August 5, 2008, 2:47am UTC](https://www.ruby-forum.com/t/rendering-a-partial-collection-with-a-layout/145003/1 "2008-08-05T02:47:33Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![zach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/51bf81/32.png) [@zach](https://www.ruby-forum.com/u/zach)\
**Post date:** [August 5, 2008, 9:46am UTC](https://www.ruby-forum.com/t/rendering-a-partial-collection-with-a-layout/145003/2 "2008-08-05T09:46:54Z")

</div>

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.
