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],
params[:session][:password])
if user.nil?
flash.now[:error] = "Invalid email/password combination."
@title = "Sign in"
render 'new'
else
# Sign the user in and redirect to the user's show page.
end
end
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???
The problem here is that your user object, isn’t retrieving any objects
on
precisely this line:
user = User.authenticate(params[:session][:email],
params[:session][:password]) 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
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???
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.
BUT…
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
name.
def self.authenticate(email, submitted_password)
user = find_by_email(email)
return nil if user.nil
return user if user.has_password?(summitted_password)
end
def self.authenticate(email, submitted_password)
user = find_by_email(email)
return nil if user.nil
return user if user.has_password?(summitted_password)
end
Here ‘user.nil’ in the third line should be ‘user.nil?’. There is a typo
(‘?’ missing).