Forum: Ruby on Rails Passing local to template to partial

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
96146b7a23174e2e024c06a49f845bb8?d=identicon&s=25 Jeffrey L. Taylor (Guest)
on 2008-12-14 05:41
(Received via mailing list)
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
26a81e5badb9e002ab9ed3542036e584?d=identicon&s=25 Michael Libby (Guest)
on 2008-12-14 13:57
(Received via mailing list)
On Sat, Dec 13, 2008 at 10:40 PM, Jeffrey L. Taylor
<ror@abluz.dyndns.org> 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:
http://api.rubyonrails.org/classes/ActionView/Base.html
Documentation on partials:
http://api.rubyonrails.com/classes/ActionView/Partials.html

Both documents show how to pass variables in.

-Michael


--
Michael C. Libby
www.mikelibby.com
96146b7a23174e2e024c06a49f845bb8?d=identicon&s=25 Jeffrey L. Taylor (Guest)
on 2008-12-14 19:45
(Received via mailing list)
Quoting Michael Libby <michael.c.libby@gmail.com>:
> Sample of the code that does not work as expected would help us help you.
>
> Documentation on templates:
> http://api.rubyonrails.org/classes/ActionView/Base.html
> Documentation on partials:
> 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
5170ca260dbd2cdfd5a887a4dba7636f?d=identicon&s=25 Jeremy Weiskotten (jeremy_weiskotten)
on 2008-12-14 19:50
Jeffrey L. Taylor wrote:
> Quoting Michael Libby <michael.c.libby@gmail.com>:
>> Sample of the code that does not work as expected would help us help you.
>>
>> Documentation on templates:
>> http://api.rubyonrails.org/classes/ActionView/Base.html
>> Documentation on partials:
>> 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
36aef9956c6082f69622dd67e8da376f?d=identicon&s=25 Joshua Abbott (joshnabbott)
on 2008-12-15 02:23
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


Jeremy Weiskotten wrote:
> Jeffrey L. Taylor wrote:
>> Quoting Michael Libby <michael.c.libby@gmail.com>:
>>> Sample of the code that does not work as expected would help us help you.
>>>
>>> Documentation on templates:
>>> http://api.rubyonrails.org/classes/ActionView/Base.html
>>> Documentation on partials:
>>> 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
This topic is locked and can not be replied to.