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],
                            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???

You can check the code in this link listing 9.8
http://ruby.railstutorial.org/chapters/sign-in-sign-out#sec:signin_failure

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],
params[:session][:password])
:slight_smile: Exactly what you where trying to do when this happened?

2012/2/3 Jean [email protected]

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

On Feb 3, 11:45am, Aline de Oliveira Freitas

On Fri, Feb 3, 2012 at 11:37, Jean [email protected] wrote:

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.

-Dave


Dave A.: Available Cleared Ruby on Rails Freelancer
(NoVa/DC/Remote) – see www.DaveAronson.com, and blogs at
www.Codosaur.us, www.Dare2XL.com, www.RecruitingRants.com

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:increate’
Request

Parameters:

{“utf8”=>“✓”,
“authenticity_token”=>“1Dc0iARaC7tLF0Jwp7ZX9XJAOTtqHqavl0+x6qkHlEw=”,
“session”=>{“email”=>"",
“password”=>"[FILTERED]"},
“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)
end

I don’t understand what’s happen

On Feb 3, 11:56am, Dave A. [email protected]

On Fri, Feb 3, 2012 at 11:53, Jean [email protected] wrote:

if I fill the field with and user and password,
that exist in my db I get the same error.

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

-Dave


Dave A.: Available Cleared Ruby on Rails Freelancer
(NoVa/DC/Remote) – see www.DaveAronson.com, and blogs at
www.Codosaur.us, www.Dare2XL.com, www.RecruitingRants.com

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).

On Fri, Feb 3, 2012 at 10:43 PM, Jean [email protected] wrote:

Request
And this is the code for user model
wrote:

Dave A.: Available Cleared Ruby on Rails Freelancer
http://groups.google.com/group/rubyonrails-talk?hl=en.


Thanks,
Prince

OMG thank you soooo much!!!

On 3 February 2012 17:13, Jean [email protected] wrote:

Request
And this is the code for user model

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

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

Colin

On Fri, Feb 3, 2012 at 12:13, Jean [email protected] wrote:

And this is the code for user model

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

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:

-Dave


Dave A.: Available Cleared Ruby on Rails Freelancer
(NoVa/DC/Remote) – see www.DaveAronson.com, and blogs at
www.Codosaur.us, www.Dare2XL.com, www.RecruitingRants.com

LOL thanks Dave!!

On Feb 3, 12:54pm, Dave A. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs