# Locals do not appear in partial unless collection is explici

**URL:** <https://www.ruby-forum.com/t/locals-do-not-appear-in-partial-unless-collection-is-explici/132699>\
**Category:** Rails\
**Created:** [March 23, 2008, 6:46pm UTC](https://www.ruby-forum.com/t/locals-do-not-appear-in-partial-unless-collection-is-explici/132699 "2008-03-23T18:46:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![raven13](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b3f665/32.png) [@raven13](https://www.ruby-forum.com/u/raven13)\
**Post date:** [March 23, 2008, 6:46pm UTC](https://www.ruby-forum.com/t/locals-do-not-appear-in-partial-unless-collection-is-explici/132699/1 "2008-03-23T18:46:50Z")

</div>

Maybe I’m misunderstanding something but it appears as if  
\<%= render :partial =\> @items, @locals =\> { :this =\> ‘that’ } %\>  
does not give the local variable this but this one does:  
\<%= render :partial =\> ‘items/item’, :collection =\> @items, @locals =\> {  
:this =\> ‘that’ } %\>  
Shouldn’t they mean the same thing?  
I’m running rails 2.0.2

Martin

---

<div class="post-metadata">

**Author:** ![raven13](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b3f665/32.png) [@raven13](https://www.ruby-forum.com/u/raven13)\
**Post date:** [March 23, 2008, 7:09pm UTC](https://www.ruby-forum.com/t/locals-do-not-appear-in-partial-unless-collection-is-explici/132699/2 "2008-03-23T19:09:17Z")

</div>

Martin LÃ¼bcke wrote:

> Maybe I’m misunderstanding something but it appears as if  
> \<%= render :partial =\> @items, @locals =\> { :this =\> ‘that’ } %\>

Don’t you want :locals =\> { …  
?

---

<div class="post-metadata">

**Author:** ![raven13](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b3f665/32.png) [@raven13](https://www.ruby-forum.com/u/raven13)\
**Post date:** [March 23, 2008, 7:16pm UTC](https://www.ruby-forum.com/t/locals-do-not-appear-in-partial-unless-collection-is-explici/132699/3 "2008-03-23T19:16:05Z")

</div>

Mark B. wrote:

> Martin LÃ¼bcke wrote:
> 
> > Maybe I’m misunderstanding something but it appears as if  
> > \<%= render :partial =\> @items, @locals =\> { :this =\> ‘that’ } %\>
> 
> Don’t you want :locals =\> { …  
> ?

Yes, that is both what I want and what I mean. It should be :locals in  
both lines, the @ was just a typo.

Martin

---

<div class="post-metadata">

**Author:** ![raven13](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b3f665/32.png) [@raven13](https://www.ruby-forum.com/u/raven13)\
**Post date:** [March 23, 2008, 7:59pm UTC](https://www.ruby-forum.com/t/locals-do-not-appear-in-partial-unless-collection-is-explici/132699/4 "2008-03-23T19:59:21Z")

</div>

On 23 Mar 2008, at 17:46, Martin Lübcke wrote:

> Maybe I’m misunderstanding something but it appears as if  
> \<%= render :partial =\> @items, @locals =\> { :this =\> ‘that’ } %\>  
> does not give the local variable this but this one does:  
> \<%= render :partial =\> ‘items/item’, :collection =\> @items, @locals  
> =\> {  
> :this =\> ‘that’ } %\>

Is @locals just a typo? Should be :locals.

Fred

---

<div class="post-metadata">

**Author:** ![raven13](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b3f665/32.png) [@raven13](https://www.ruby-forum.com/u/raven13)\
**Post date:** [March 23, 2008, 7:59pm UTC](https://www.ruby-forum.com/t/locals-do-not-appear-in-partial-unless-collection-is-explici/132699/5 "2008-03-23T19:59:22Z")

</div>

On 23 Mar 2008, at 18:58, Frederick C. wrote:

> Is @locals just a typo? Should be :locals.

Oops, ignore me, being a bit slow today.

Fred

---

<div class="post-metadata">

**Author:** ![raven13](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b3f665/32.png) [@raven13](https://www.ruby-forum.com/u/raven13)\
**Post date:** [March 23, 2008, 8:35pm UTC](https://www.ruby-forum.com/t/locals-do-not-appear-in-partial-unless-collection-is-explici/132699/6 "2008-03-23T20:35:20Z")

</div>

Martin LÃ¼bcke wrote:

> > > \<%= render :partial =\> @items, @locals =\> { :this =\> ‘that’ } %\>
> 
> Yes, that is both what I want and what I mean. It should be :locals in  
> both lines, the @ was just a typo.

ok 🙂

Looking at the code for render\_partial (abbreviated):

def render\_partial(partial\_path, object\_assigns = nil, local\_assigns =  
nil)  
case partial\_path  
when String, Symbol, NilClass  
:  
local\_assigns = local\_assigns ? local\_assigns.clone : {}  
:  
render("#{path}/\_#{partial\_name}", local\_assigns)  
:  
when Array, ActiveRecord::Associations::AssociationCollection,  
ActiveRecord::Associations::HasManyThroughAssociation  
:  
render\_partial\_collection(path, collection, nil,  
object\_assigns.value)  
:

So, if you pass an array or an association collection (such as @items  
above) as the value of the :partial key, then local\_assigns is _not_  
passed through. Unless I’m reading this all wrong.

I have not looked to see if this is expected behaviour or a bug…

---

<div class="post-metadata">

**Author:** ![raven13](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b3f665/32.png) [@raven13](https://www.ruby-forum.com/u/raven13)\
**Post date:** [March 23, 2008, 8:45pm UTC](https://www.ruby-forum.com/t/locals-do-not-appear-in-partial-unless-collection-is-explici/132699/7 "2008-03-23T20:45:44Z")

</div>

Mark B. wrote:

> def render\_partial(partial\_path, object\_assigns = nil, local\_assigns =  
> nil)  
> :  
> render\_partial\_collection(path, collection, nil,  
> object\_assigns.value)  
> :

To complete the picture here, the definition of render goes:

def render(options = {}, old\_local\_assigns = {}, &block) #:nodoc:  
:  
elsif options.is\_a?(Hash)  
:  
elsif options[:partial] && options[:collection]  
render\_partial\_collection(options[:partial], options[:collection],  
options[:spacer\_template],  
options[:locals])  
elsif options[:partial]  
render\_partial(options[:partial],  
ActionView::Base::ObjectWrapper.new(options[:object]),  
options[:locals])

So yes, things work differently for partials if a collection is  
supplied. Note that options[:locals] is supplied to #render\_partial but  
the argument in my previous post shows why this is not passed through to  
#render\_partial\_collection, but it is if options[:collection] is  
defined.
