Forum: Ruby on Rails Redirect if session is nil

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.
Jasbur (Guest)
on 2006-06-08 02:04
How exactly do i redirect a user if they're not logged in?

I have a cookie being written to the @session variable on login and I
can pull info from it. For instance "@session['user'].login" pulls their
username just fine. But if they type in the url of the "welcome" page
without logging in I get "You have a nil object when you didn't expect
it!" which I would expect.

Instead of this nasty error message, how would I redirect them to the
login page?
Daniel H. (Guest)
on 2006-06-08 02:10
(Received via mailing list)
Probably something like

if session['user'].nil?
	redirect_to whatever
end

You would probably want to make this into a filter, too
Jasbur (Guest)
on 2006-06-08 02:19
It worked! It's too simple I think that's the problem. It's amazing what
5 hours of coding will do to the hum brain. Thanks.
This topic is locked and can not be replied to.