0

I'm following the Michael Hartl tutorial and I got stuck in chapter 9.

I need this function to work:

def signed_in? !current_user end

but this function always return false even when I'm logged. I'm sorry for this simple question, but I'm a begginer and also spend a long time trying figure it out. I'm using ruby 1.8.7 and rails 2.3.8

thanks in advanced!

3 Answers 3

1

I suspect the code should read

def signed_in?
  current_user
end

Putting a ! in front of the call to current_user can be read as "return true if it is NOT the current_user".

Your code inside of SessionsHelper has a typo. The :remember_token cookie is not getting saved off in the sign_in method. It should read:

def sign_in(user)
  user.remember_me!
  cookies[:remember_token] = { :value => user.remember_token,
                               :expires => 20.years.from_now.utc }
  self.current_user = user
end

Not :remember_toker.

5
  • Hi, I tried with !current_user, also with !!current_user and withou any !. Nothing worked. I still can't see if I'm logged or not.
    – Heber
    Jan 19, 2011 at 15:13
  • I wrote wrong here, I actually write exactly the same as in the tutorial. I tested in this way: !current_user.nil?, but it is not working.
    – Heber
    Jan 19, 2011 at 15:22
  • Edit your question and post the code to current_user as well as where you are calling signed_in?. Something is likely wrong there. Jan 19, 2011 at 15:36
  • Hi Aaron, I pushed my source to github at github.com/heber28/sample_app, please, checkout the file sample_app/app/helpers/sessions_helper.rb and sample_app/app/views/layouts/_header.html.erb. My question is, why current_user is always nil if in the session helper it put a value into it.
    – Heber
    Jan 19, 2011 at 16:19
  • You're welcome. You should accept the answer as it will help your approval rating. Jan 19, 2011 at 20:06
1

You have to add a .nil? to current_user like this:

def signed_in?
    !current_user.nil?
end
0

I had a similar issue and decided to work backwards.

1) def signed_in? !current_user.nil? end. So I looked for the declaration & definition of current_user associated with a sign-in

2) Here: def sign_in(user) cookies.permanent.signed[:remember_token] = [user.id, user.salt] self.current_user = user end. For me, token was misspelled and that fixed it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.