Variables in layouts


#1

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


#2

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 ?


#3

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


#4

On 1/4/06, Gareth R. removed_email_address@domain.invalid 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.

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

<%= username %>

#5

Gareth R. wrote:

It seems that a better way would be to use an instance variable like I

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


#6

On Jan 4, 2006, at 8:31 AM, Lance B. wrote:

end

/app/views/layouts/application.rhtml

<%= username %>

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 Z.
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
removed_email_address@domain.invalid


#7

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


#8

Kevin O. 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/shared

These 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