I have a layout that provides the basic outline of each page in my app. It has a title, footer and a navigation bar. In the title I want to put the name of the current user that is logged in. I could do something like @session[:username] but I would rather encapsulate that into a single variable so that I can change the key in the session map if I want without having to goto each page that uses it. It seems that a better way would be to use an instance variable like I have seen @page_title a lot. I dont want to set this in each and every action so is there a way that I can set it in the super class controller? Maybe in after_filter? What do you guys do? Thanks Gareth -- Posted via http://www.ruby-forum.com/.
> It seems that a better way would be to use an instance variable like I > have seen @page_title a lot. I dont want to set this in each and every > action so is there a way that I can set it in the super class > controller?In ApplicationController ? -- Posted via http://www.ruby-forum.com/.
Gareth Reeves wrote:>I have a layout that provides the basic outline of each page in my app. >It has a title, footer and a navigation bar. > >In the title I want to put the name of the current user that is logged >in. > >I could do something like @session[:username] but I would rather >encapsulate that into a single variable so that I can change the key in >the session map if I want without having to goto each page that uses it. > >It seems that a better way would be to use an instance variable like I >have seen @page_title a lot. I dont want to set this in each and every >action so is there a way that I can set it in the super class >controller? > >Maybe in after_filter? What do you guys do? > >Thanks > >Gareth > > >class ApplicationController < ActionController::Base before_filter :setup_user private def setup_user @user = @session[''user''] end end -- For rails performance tuning, see: http://railsexpress.de/blog Subscription: http://railsexpress.de/blog/xml/rss20/feed.xml
On 1/4/06, Gareth Reeves <reevesg@pobox.com> wrote:> In the title I want to put the name of the current user that is logged > in. > > I could do something like @session[:username] but I would rather > encapsulate that into a single variable so that I can change the key in > the session map if I want without having to goto each page that uses it.<snip> What about something like this: /app/helpers/application_helper.rb def username if @session.nil? "Guest" else @session[:username] end end /app/views/layouts/application.rhtml <title><%= username %></title>
Lieven De Keyzer wrote:>> It seems that a better way would be to use an instance variable like I >> have seen @page_title a lot. I dont want to set this in each and every >> action so is there a way that I can set it in the super class >> controller? > > In ApplicationController ?I dont understand what you are asking. I have a superclass for each controller that has the before_filter in it that checks to see if the users is logged in and if not redirects to the login page. This feels like the right place to set some of those intstance variables e.g. @username. I was interested in others have solved this problem. Gareth -- Posted via http://www.ruby-forum.com/.
On Jan 4, 2006, at 8:31 AM, Lance Ball wrote:> On 1/4/06, Gareth Reeves <reevesg@pobox.com> wrote: >> In the title I want to put the name of the current user that is >> logged >> in. >> >> I could do something like @session[:username] but I would rather >> encapsulate that into a single variable so that I can change the >> key in >> the session map if I want without having to goto each page that >> uses it. > <snip> > > What about something like this: > > /app/helpers/application_helper.rb > def username > if @session.nil? > "Guest" > else > @session[:username] > end > end > > /app/views/layouts/application.rhtml > <title><%= username %></title>Note that the above won''t work all the time. You need to check for session[:username].nil? and not just session.nil? because the flash and all kinds of other things will go into the session making it not nil most of the time. SO be careful with that. Cheers- -Ezra Zygmuntowicz Yakima Herald-Republic WebMaster http://yakimaherald.com 509-577-7732 ezra@yakima-herald.com
Why not use a partial template for the user information? So long as you include that, all you would need to change is the partial and everything else will update. <%= render(:partial=>''shared/userinfo'') %> Just put _userinfo.rhtml in app/views/shared -- Posted via http://www.ruby-forum.com/.
Kevin Olbrich wrote:> Why not use a partial template for the user information? So long as you > include that, all you would need to change is the partial and everything > else will update. > > <%= render(:partial=>''shared/userinfo'') %> > > Just put _userinfo.rhtml in app/views/sharedThese are all great suggestions guys and exactly what I was looking for. I particularly like the helper suggestion and think that will suit my needs best for the moment and then maybe move to the partial template. Thanks again. Gareth -- Posted via http://www.ruby-forum.com/.