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.
Aa575b7606897a056f073f0f148804dd?d=identicon&s=25 Gareth Reeves (Guest)
on 2006-01-04 17: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
F66cc63471b71843dba49db8e2b3771d?d=identicon&s=25 Lieven De keyzer (lieven)
on 2006-01-04 17: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 ?
Aa575b7606897a056f073f0f148804dd?d=identicon&s=25 Gareth Reeves (Guest)
on 2006-01-04 18: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
E555e7c34196967444a47a96395a23ab?d=identicon&s=25 Stefan Kaes (Guest)
on 2006-01-04 18:13
(Received via mailing list)
Gareth Reeves 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
D046cca1a33655b6285065ec89711389?d=identicon&s=25 Lance Ball (Guest)
on 2006-01-04 18:13
(Received via mailing list)
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>
132a94ca65959bda6c74fae54bff2425?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-01-04 19:45
(Received via mailing list)
On Jan 4, 2006, at 8:31 AM, Lance Ball 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 Zygmuntowicz
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
ezra@yakima-herald.com
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-01-04 20: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
Aa575b7606897a056f073f0f148804dd?d=identicon&s=25 Gareth Reeves (Guest)
on 2006-01-04 20:23
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/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.