Forum: Ruby on Rails session[:user_id] / name problem! Help!

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.
1e2dd87a31c5d3e07adc9efa375f4dc4?d=identicon&s=25 ryan (Guest)
on 2006-05-08 18:30
Ok, I have a login on my site, and when I log in, I want to display
"Welcome #{user}".

In my session, if I do "Welcome <%= session[:user_id] %>" I'll get
"Welcome 1" which is right, but how can I access the name based on the
session id?

For instance, essentially I want to do this:

"Welcome <%= session[:user_name] %>"

or

"Welcome <%= session[:user_id].name %>"

You can see what I'm trying, but I'm fairly new to all of this, so I'm
sure these things seem ridiculous to you.  I've been reading the Agile
Web Dev with RoR book, so I'm learning, but still can't seem to get
this.

Please Help!  Thanks a lot...
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-05-08 18:51
(Received via mailing list)
On Monday, May 08, 2006, at 6:30 PM, ryan wrote:
>
>
>--
>Posted via http://www.ruby-forum.com/.
>_______________________________________________
>Rails mailing list
>Rails@lists.rubyonrails.org
>http://lists.rubyonrails.org/mailman/listinfo/rails
try

<%= User.find(session[:user_id]).name %>

You can save the entire user object in the session, but if you do that
you may also save all their associated objects as well.  This could get
big.  You would also need to reload the User object or changes to it may
go undetected.

An alternative would be to simply store the user name in the session at
the same time you write the user_id, so you might have something like
'session[:user_name]' to call on.  This might be preferable since it
prevents a database query just to print the user name.

_Kevin
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (Guest)
on 2006-05-08 19:08
ryan wrote:
> Ok, I have a login on my site, and when I log in, I want to display
> "Welcome #{user}".
>
> In my session, if I do "Welcome <%= session[:user_id] %>" I'll get
> "Welcome 1" which is right, but how can I access the name based on the
> session id?
>
> For instance, essentially I want to do this:
>
> "Welcome <%= session[:user_name] %>"
>
> or
>
> "Welcome <%= session[:user_id].name %>"
>
> You can see what I'm trying, but I'm fairly new to all of this, so I'm
> sure these things seem ridiculous to you.  I've been reading the Agile
> Web Dev with RoR book, so I'm learning, but still can't seem to get
> this.
>
> Please Help!  Thanks a lot...

I usually create a before filter that fetches my user.  So in
app/controllers/application.rb

  before_filter :get_user
  def get_user
    @user = User.find(session[:user_id]) if session[:user_id]
  end

Now in any views you can do:

  Welcome <%= @user ? @user.name : 'Guest' %>!

Note that on any page that can be seen by logged in people and
non-logged in people, you have to make sure that the @user object
exists, other you will be calling the "name" method on "nil" which would
raise a big ugly error.
This topic is locked and can not be replied to.