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

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

[email protected] 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

Daniel B. wrote:

[email protected] 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

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 forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs