Redirect if session is nil


#1

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?


#2

Probably something like

if session[‘user’].nil?
redirect_to whatever
end

You would probably want to make this into a filter, too


#3

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.