Forum: Ruby on Rails variables in layouts

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Gareth R. (Guest)
on 2006-01-04 18:21
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
Lieven D. (Guest)
on 2006-01-04 18:27
> 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 ?
Gareth R. (Guest)
on 2006-01-04 19:09
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
Stefan K. (Guest)
on 2006-01-04 19:13
(Received via mailing list)
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
Lance B. (Guest)
on 2006-01-04 19:13
(Received via mailing list)
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.
<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>
Ezra Z. (Guest)
on 2006-01-04 20:45
(Received via mailing list)
On Jan 4, 2006, at 8:31 AM, Lance B. wrote:

> <snip>
>   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 Z.
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
removed_email_address@domain.invalid
Kevin O. (Guest)
on 2006-01-04 21:11
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
Gareth R. (Guest)
on 2006-01-04 21:23
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
This topic is locked and can not be replied to.