Forum: Ruby on Rails Should partials have access to the same var's as the view?

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.
167a3b5582230623eb94e638133122ec?d=identicon&s=25 Kris@AFM (Guest)
on 2006-05-31 15:52
I have a view, part of it I put in to a partial - a copy paste - but now
I get an error saying that there is an "undefined local variable or
method" in the partial code...

Do I need to pass my var to the partial, I would have though not?

Cheers AFM
16b26104470ae1b64958f3caa836c193?d=identicon&s=25 Danny Burkes (dburkes)
on 2006-05-31 18:11
Kris@AFM wrote:
> I have a view, part of it I put in to a partial - a copy paste - but now
> I get an error saying that there is an "undefined local variable or
> method" in the partial code...
>
> Do I need to pass my var to the partial, I would have though not?
>
> Cheers AFM

You need to pass your variables to the partial using the :locals key,
like this:

render :partial => 'my_partial', :locals => { :foo => 'bar', :fro =>
'baz' }


Then, your partial can access the local variables 'foo' and 'fro'.

- Danny
167a3b5582230623eb94e638133122ec?d=identicon&s=25 AFM (Guest)
on 2006-06-28 12:37
Daniel Burkes wrote:
> Kris@AFM wrote:
>> I have a view, part of it I put in to a partial - a copy paste - but now
>> I get an error saying that there is an "undefined local variable or
>> method" in the partial code...
>>
>> Do I need to pass my var to the partial, I would have though not?
>>
>> Cheers AFM
>
> You need to pass your variables to the partial using the :locals key,
> like this:
>
> render :partial => 'my_partial', :locals => { :foo => 'bar', :fro =>
> 'baz' }

This works, thanks. I just assumed partials worked like includes, they
must be rendered independantly from the master view.

>
>
> Then, your partial can access the local variables 'foo' and 'fro'.
>
> - Danny
2da252cc1d2fb3701126171831b50a65?d=identicon&s=25 Dave Coleman (pezdude)
on 2006-06-28 16:22
> This works, thanks. I just assumed partials worked like includes, they
> must be rendered independantly from the master view.

You should only have to pass vars to a partial if they're local. You can
read globals and instance variables from your partial without passing
the vars to the partial.

       bob  # local var
       @bob # instance var
       $bob # global var
This topic is locked and can not be replied to.