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... -- Posted via http://www.ruby-forum.com/.
On Monday, May 08, 2006, at 6:30 PM, 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... > >-- >Posted via http://www.ruby-forum.com/. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/railstry <%= 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 -- Posted with http://DevLists.com. Sign up and save your mailbox.
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. -- Posted via http://www.ruby-forum.com/.