I am new to Ruby on Rails in general and apologize if this question is trivial and/or has been asked before ( my searches did not turn up any threads ). I am using the cookies function from actionpack which I have located in my vendor/rails/actionpack directory. Using: cookies[:username] and cookies[:password] from within a controller file works just fine, I am able to set and read cookies. I would like to add cookie functionality to the overall template that my controllers are using via layout "template", however, trying to access cookies[] causes the following error: --- undefined local variable or method `cookies'' for #<#<Class:0x40a87040>:0x40a86d0c> --- I am missing something as I suspect that I should be able to access the external libs I have stored in vendor but I cannot put my finger on what I need to do. Any help would be greatly appreciated! -Andy -- Posted via http://www.ruby-forum.com/.
Francois Beausoleil
2006-Apr-03 02:10 UTC
[Rails] Layout Question w/ code from vendor library
Hello Andrew, 2006/4/2, Andrew Cowan <icculus@gmdstudios.com>:> I would like to add cookie functionality to the overall template that my > controllers are using via layout "template", however, trying to access > cookies[] causes the following error: > > --- > undefined local variable or method `cookies'' for > #<#<Class:0x40a87040>:0x40a86d0c> > ---You should not be accessing cookies from your views. Instead, the controller should make any necessary values available to the view. The view shouldn''t need to know where the values come from. That being said, you can probably use @cookies in the view, if you really want. Haven''t tried it. Bye ! -- Fran?ois Beausoleil http://blog.teksol.info/
Andrew Cowan
2006-Apr-03 03:09 UTC
[Rails] Re: Layout Question w/ code from vendor library
Well, for example if I want to check the ''logged_in'' status of a user based on cookied username/password and display a message such as: "Welcome back (username)" (or) "You are not logged in, please login here" it makes sense to me that such a message should be provided in the overall template specified by the layout, rather than have every action''s view format the message and display it. Is there a way to avoid having to re-use the code to check login state and prepare the message for every specific view ? Thanks -Andy Fran?ois Beausoleil wrote:> Hello Andrew, > > 2006/4/2, Andrew Cowan <icculus@gmdstudios.com>: >> I would like to add cookie functionality to the overall template that my >> controllers are using via layout "template", however, trying to access >> cookies[] causes the following error: >> >> --- >> undefined local variable or method `cookies'' for >> #<#<Class:0x40a87040>:0x40a86d0c> >> --- > > You should not be accessing cookies from your views. Instead, the > controller should make any necessary values available to the view. > The view shouldn''t need to know where the values come from. > > That being said, you can probably use @cookies in the view, if you > really want. Haven''t tried it. > > Bye !-- Posted via http://www.ruby-forum.com/.
Why not save this kind of information in the session. The session usually tracks which user is currently logged in for the session in the various user management plugins. then in your view or controller session[''user''] There are many systems that do this, although they usually provide a helper method for the views. ie def current_user session[''user''] end then in your view call curren_user The acts_as_authenticated plugin is a good place to start for user logins and such. Cheers On 4/3/06, Andrew Cowan <icculus@gmdstudios.com> wrote:> > > Well, for example if I want to check the ''logged_in'' status of a user > based on cookied username/password and display a message such as: > > "Welcome back (username)" > > (or) > > "You are not logged in, please login here" > > it makes sense to me that such a message should be provided in the > overall template specified by the layout, rather than have every > action''s view format the message and display it. > > Is there a way to avoid having to re-use the code to check login state > and prepare the message for every specific view ? > > Thanks > -Andy > > > Fran?ois Beausoleil wrote: > > Hello Andrew, > > > > 2006/4/2, Andrew Cowan <icculus@gmdstudios.com>: > >> I would like to add cookie functionality to the overall template that > my > >> controllers are using via layout "template", however, trying to access > >> cookies[] causes the following error: > >> > >> --- > >> undefined local variable or method `cookies'' for > >> #<#<Class:0x40a87040>:0x40a86d0c> > >> --- > > > > You should not be accessing cookies from your views. Instead, the > > controller should make any necessary values available to the view. > > The view shouldn''t need to know where the values come from. > > > > That being said, you can probably use @cookies in the view, if you > > really want. Haven''t tried it. > > > > Bye ! > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060403/910f4c83/attachment-0001.html
Andrew Cowan
2006-Apr-03 04:41 UTC
[Rails] Re: Re: Layout Question w/ code from vendor library
Thank you, I will look into this solution! :) -Andy Daniel ----- wrote:> Why not save this kind of information in the session. > > The session usually tracks which user is currently logged in for the > session > in the various user management plugins. > > then in your view or controller > session[''user''] > > There are many systems that do this, although they usually provide a > helper > method for the views. > > ie > def current_user > session[''user''] > end > > then in your view call curren_user > > The acts_as_authenticated plugin is a good place to start for user > logins > and such. > > Cheers-- Posted via http://www.ruby-forum.com/.