ruby on rails 3 tutorial book chapter9 Signin Failure

Hi I have problem trying to pass the signin failure test.

This is the test code of the sessions_controller.rb

       def create
       user = User.authenticate(params[:session][:email],
       if user.nil?[:error] = "Invalid email/password combination."
         @title = "Sign in"
         render 'new'
         # Sign the user in and redirect to the user's show page.

If I check in my browser and clic on submit with all fields empty, I
get the following error:

NoMethodError in SessionsController
#create undefined method `nil' for nil:NilClass

I check the book many times and I have code exactly as the book, but I
still have the error. Any help???

You can check the code in this link listing 9.8

What I doing wrong?

Thanks for any help.

The problem here is that your user object, isn’t retrieving any objects on precisely this line:

user = User.authenticate(params[:session][:email],

:slight_smile: Exactly what you where trying to do when this happened?

Well I tried two things. Go to localhost:3000/signin click on "sign
in" button with out any information on email and password field, and I
get this error. But if I fill the field with and user and password,
that exist in my db I get the same error. And in the book at this time
I suppost to see a flash error with something like a "invalid email/
password combination" but I get this error :s

It is a bit odd. When we see "undefined method `some_method_name' for
nil:NilClass" that usually means that something was nil unexpectedly.
This time though, the method called, according to the code, is "nil?",
which does indeed exist on NilClass, and the code is indeed properly
checking for a nil user.


Then look a bit more closely at the error code. It's gritching about
"nil", not "nil?". Are you absolutely sure that the code you posted,
is what is executing? Maybe you copied from the book into the post,
but when you copied by hand from the book into your actual app, you
forgot the question mark? That's an easy thing to miss, especially if
you're not used to a question mark being a valid part of a method


That also sounds like a forgotten question mark. Ruby would try to
call it, and fail, either way.


No Dave, I copy from my code. And this is the error:

NoMethodError in SessionsController#create

undefined method `nil' for nil:NilClass
Rails.root: /Users/jeanosorio/rails_projects/sample_app

Application Trace | Framework Trace | Full Trace
app/models/user.rb:37:in `authenticate'
app/controllers/sessions_controller.rb:7:in `create'


"commit"=>"Sign in"}

And this is the code for user model

def self.authenticate(email, submitted_password)
    user = find_by_email(email)
    return nil if user.nil
    return user if user.has_password?(summitted_password)

I don't understand what's happen

def self.authenticate(email, submitted_password)
user = find_by_email(email)
return nil if user.nil
return user if user.has_password?(summitted_password)

Here ‘user.nil’ in the third line should be ‘user.nil?’. There is a typo (’?’ missing).

Read the earlier posts from Dave carefully and look at the line above.


OMG thank you soooo much!!!

Aha! Right error, wrong place. Here's where you're missing the
question mark. You had me worried that my awesome psychic powers were
on the blink. :wink:


LOL thanks Dave!!