Just to followup if you’re interested, … the error msg from the op
was due to the fact that the call to session[:user_id] returned nil,
and thus calling find(nil) resulted in that error being raised. If
you wanted to avoid such an error and just have the find call return
nil if not found, one way is to just call find_by_id:
$ ./script/console
u = User.find(nil)
ActiveRecord::RecordNotFound: Couldn’t find User without an ID
from /usr/lib/ruby/…
…
You need to force a redirect to a login page if there is no session
[:user_id] and you should also handle the fact that session[:user_id]
may be nil in your is_logged_in? method.
Its saved in the login method:
user = User.find_by_user_name(@user.user_name)
if user and user.password_matches?(@user.password)
user.login!(session)
if @user.remember_me == "1"
cookies[:remember_me] = { :value => "1",
:expires => 10.years.from_now }
user.authorization_token = Digest::SHA1.hexdigest(
"#{user.user_name}:#{user.password}")
I have a redirect if someone tries to access the admin privileges if
thats what you mean?
You need to force a redirect to a login page if there is no session
[:user_id] and you should also handle the fact that session[:user_id]
may be nil in your is_logged_in? method.
Just to clarify - are you setting session[:user_id] in the user.login!
(session) call?
The other suggestions on handling a nil session[:user_id] (i.e. using
find_by_id) are the appropriate ways to handle the error.
Just to clarify - are you setting session[:user_id] in the user.login!
(session) call?
The other suggestions on handling a nil session[:user_id] (i.e. using
find_by_id) are the appropriate ways to handle the error.