Should partials have access to the same var's as the view?


#1

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


#2

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

#3

Daniel B. 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

#4

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