Kris@AFM
2006-May-31 13:52 UTC
[Rails] 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 -- Posted via http://www.ruby-forum.com/.
Daniel Burkes
2006-May-31 16:12 UTC
[Rails] Re: Should partials have access to the same var''s as the vie
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 AFMYou 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 -- Posted via http://www.ruby-forum.com/.
AFM
2006-Jun-28 10:38 UTC
[Rails] Re: Should partials have access to the same var''s as the vie
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-- Posted via http://www.ruby-forum.com/.
David C.
2006-Jun-28 14:23 UTC
[Rails] Re: Should partials have access to the same var''s as the vie
> 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 -- Posted via http://www.ruby-forum.com/.