# Passing local to template to partial

**URL:** <https://www.ruby-forum.com/t/passing-local-to-template-to-partial/155590>\
**Category:** Rails\
**Created:** [December 14, 2008, 5:41am UTC](https://www.ruby-forum.com/t/passing-local-to-template-to-partial/155590 "2008-12-14T05:41:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeffrey\_LSTaylor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ee7513/32.png) [@Jeffrey\_LSTaylor](https://www.ruby-forum.com/u/Jeffrey_LSTaylor)\
**Post date:** [December 14, 2008, 5:41am UTC](https://www.ruby-forum.com/t/passing-local-to-template-to-partial/155590/1 "2008-12-14T05:41:13Z")

</div>

I want to pass an array to a template as a local variable. The template  
passes the same local variable to a partial as a local variable. This  
isn’t  
working as I expect and something in the back of my mind is says that I  
read  
something about this. Nothing shows up in the first three pages of my  
Google  
search and it’s degenerating into Japanese train (rail) passes.

Help,  
Jeffrey

---

<div class="post-metadata">

**Author:** ![Jeffrey\_LSTaylor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ee7513/32.png) [@Jeffrey\_LSTaylor](https://www.ruby-forum.com/u/Jeffrey_LSTaylor)\
**Post date:** [December 14, 2008, 1:57pm UTC](https://www.ruby-forum.com/t/passing-local-to-template-to-partial/155590/2 "2008-12-14T13:57:52Z")

</div>

On Sat, Dec 13, 2008 at 10:40 PM, Jeffrey L. Taylor  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I want to pass an array to a template as a local variable. The template  
> passes the same local variable to a partial as a local variable. This isn’t  
> working as I expect and something in the back of my mind is says that I read  
> something about this. Nothing shows up in the first three pages of my Google  
> search and it’s degenerating into Japanese train (rail) passes.

Sample of the code that does not work as expected would help us help  
you.

Documentation on templates:

> **[ActionView::Base](https://api.rubyonrails.org/classes/ActionView/Base.html)**
>
> Action View Base Action View templates can be written in several ways. If the template file has a .erb extension, then it uses the erubi template system which can embed Ruby into an HTML document.

Documentation on partials:  
[http://api.rubyonrails.com/classes/ActionView/Partials.html](http://api.rubyonrails.com/classes/ActionView/Partials.html)

Both documents show how to pass variables in.

-Michael

–  
Michael C. Libby  
[www.mikelibby.com](http://www.mikelibby.com)

---

<div class="post-metadata">

**Author:** ![Jeffrey\_LSTaylor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ee7513/32.png) [@Jeffrey\_LSTaylor](https://www.ruby-forum.com/u/Jeffrey_LSTaylor)\
**Post date:** [December 14, 2008, 7:45pm UTC](https://www.ruby-forum.com/t/passing-local-to-template-to-partial/155590/3 "2008-12-14T19:45:21Z")

</div>

Quoting M. Libby [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Sample of the code that does not work as expected would help us help you.
> 
> Documentation on templates:  
> [ActionView::Base](http://api.rubyonrails.org/classes/ActionView/Base.html)  
> Documentation on partials:  
> [Peak Obsession](http://api.rubyonrails.com/classes/ActionView/Partials.html)
> 
> Both documents show how to pass variables in.

But neither show passing locals to actions, e.g.

render :action =\> ‘new’, :locals =\> {:universe =\> ‘abc’}

I have looked at a bunch of examples and none do this.

In fact, I think the first URL lists obsolete APIs. Where it reads:

render “shared/header”, { :headline =\> “Welcome”, :person =\> person }

I believe the correct API for recent versions of Rails (I’m using 2.1.2)  
is:

render “shared/header”, :locals=\>{:headline =\> “Welcome”, :person =\>  
person}

From my experiments, it appears passing locals to actions in  
unsupported.  
I’ve changed to passing the needed information thru an instance variable  
to  
the action template and a local variable to the partial.

Thanks,  
Jeffrey

---

<div class="post-metadata">

**Author:** ![Jeffrey\_LSTaylor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ee7513/32.png) [@Jeffrey\_LSTaylor](https://www.ruby-forum.com/u/Jeffrey_LSTaylor)\
**Post date:** [December 14, 2008, 7:50pm UTC](https://www.ruby-forum.com/t/passing-local-to-template-to-partial/155590/4 "2008-12-14T19:50:17Z")

</div>

Jeffrey L. Taylor wrote:

> Quoting M. Libby [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > Sample of the code that does not work as expected would help us help you.
> > 
> > Documentation on templates:  
> > [ActionView::Base](http://api.rubyonrails.org/classes/ActionView/Base.html)  
> > Documentation on partials:  
> > [Peak Obsession](http://api.rubyonrails.com/classes/ActionView/Partials.html)
> > 
> > Both documents show how to pass variables in.
> 
> But neither show passing locals to actions, e.g.
> 
> render :action =\> ‘new’, :locals =\> {:universe =\> ‘abc’}
> 
> I have looked at a bunch of examples and none do this.
> 
> In fact, I think the first URL lists obsolete APIs. Where it reads:
> 
> render “shared/header”, { :headline =\> “Welcome”, :person =\> person }
> 
> I believe the correct API for recent versions of Rails (I’m using 2.1.2)  
> is:
> 
> render “shared/header”, :locals=\>{:headline =\> “Welcome”, :person =\>  
> person}
> 
> From my experiments, it appears passing locals to actions in  
> unsupported.  
> I’ve changed to passing the needed information thru an instance variable  
> to  
> the action template and a local variable to the partial.
> 
> Thanks,  
> Jeffrey

No, I don’t think you can pass locals to render :action.

Try render :partial =\> ‘shared/header’, :locals =\> {…}

Note that the partial filename must be /views/shared/\_header.html.erb

---

<div class="post-metadata">

**Author:** ![Jeffrey\_LSTaylor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ee7513/32.png) [@Jeffrey\_LSTaylor](https://www.ruby-forum.com/u/Jeffrey_LSTaylor)\
**Post date:** [December 15, 2008, 2:23am UTC](https://www.ruby-forum.com/t/passing-local-to-template-to-partial/155590/5 "2008-12-15T02:23:11Z")

</div>

You can pass the variable to the action with an instance variable, and  
from there you can pass the instance variable to the partial, I would  
think.

def whatever  
@locals =\> { :universe =\> ‘abc’ }  
render :action =\> ‘new’  
end

Hope that helps.

– Josh  
[http://iammrjoshua.com](http://iammrjoshua.com)

Jeremy Weiskotten wrote:

> Jeffrey L. Taylor wrote:
> 
> > Quoting M. Libby [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> > 
> > > Sample of the code that does not work as expected would help us help you.
> > > 
> > > Documentation on templates:  
> > > [ActionView::Base](http://api.rubyonrails.org/classes/ActionView/Base.html)  
> > > Documentation on partials:  
> > > [Peak Obsession](http://api.rubyonrails.com/classes/ActionView/Partials.html)
> > > 
> > > Both documents show how to pass variables in.
> > 
> > But neither show passing locals to actions, e.g.
> > 
> > render :action =\> ‘new’, :locals =\> {:universe =\> ‘abc’}
> > 
> > I have looked at a bunch of examples and none do this.
> > 
> > In fact, I think the first URL lists obsolete APIs. Where it reads:
> > 
> > render “shared/header”, { :headline =\> “Welcome”, :person =\> person }
> > 
> > I believe the correct API for recent versions of Rails (I’m using 2.1.2)  
> > is:
> > 
> > render “shared/header”, :locals=\>{:headline =\> “Welcome”, :person =\>  
> > person}
> > 
> > From my experiments, it appears passing locals to actions in  
> > unsupported.  
> > I’ve changed to passing the needed information thru an instance variable  
> > to  
> > the action template and a local variable to the partial.
> > 
> > Thanks,  
> > Jeffrey
> 
> No, I don’t think you can pass locals to render :action.
> 
> Try render :partial =\> ‘shared/header’, :locals =\> {…}
> 
> Note that the partial filename must be /views/shared/\_header.html.erb
