Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby User Authentication with Rails Password Hashing and Sign In Creating the Sessions Controller

Graham Grochowski
Graham Grochowski
4,676 Points

undefined method `authenticate' for nil:NilClass

Ive spent probably an hour fiddling around with my code and cannot for the life of me figure out why this test wont pass.

Ive uploaded this as a github repo https://github.com/ggrochow/odot

Errors im getting

) UserSessionsController POST 'create' with correct credentials redirects to the todo list path
 Failure/Error: post :create, email: "jason@teamtreehouse.com", password: "treehouse1"
 NoMethodError:
   undefined method `authenticate' for nil:NilClass
 # ./app/controllers/user_sessions_controller.rb:7:in `create'
 # ./spec/controllers/user_sessions_controller_spec.rb:22:in `block (4 levels) in <top (required)>'

1 Answer

Seth Reece
Seth Reece
32,867 Points

Your code looks fine to me. Could be your bcrypt-ruby gem, or maybe a typo in your user controller for has_secure_password? Is your project in workspaces to be forked, or on github?

Seth Reece
Seth Reece
32,867 Points

Hmm.. everything still looks fine. I assume you wouldn't have gotten this far without having the bcrypt gem installed.

Graham Grochowski
Graham Grochowski
4,676 Points

https://github.com/ggrochow/odot Uploaded my project files so far up to github, appreciate the help so far. Everything's been working fine up to this point, and i've been able to google around and figure out any other errors ive had, this ones stumped me though.

Seth Reece
Seth Reece
32,867 Points

It appears that the ! at the end of self.email = email.downcase is preventing the email address from saving in the database. The user tests pass because it is sending in the right information.

Started POST "/users" for 127.0.0.1 at 2015-10-06 15:06:25 -0700
Processing by UsersController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"YGmKt135ndHaBhz03LaLZ5gU2280lwYQ9hSrJL/zHGA=", "user"=>{"first_name"=>"Test", "last_name"=>"User", "email"=>"test@test.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign Up"}
   (0.1ms)  begin transaction
  User Exists (0.1ms)  SELECT 1 AS one FROM "users" WHERE "users"."email" = 'test@test.com' LIMIT 1
Binary data inserted for `string` type on column `password_digest`

but the SQL that runs is

SQL (0.3ms)  INSERT INTO "users" ("created_at", "first_name", "last_name", "password_digest", "updated_at") VALUES (?, ?, ?, ?, ?)  [["created_at", Tue, 06 Oct 2015 22:06:26 UTC +00:00], ["first_name", "Test"], ["last_name", "User"], ["password_digest", "$2a$10$Sx9ivfhYYM1gW.VhRLdw6es8hgKL3MdycsA9tfw24gHqsDMxblKd6"], ["updated_at", Tue, 06 Oct 2015 22:06:26 UTC +00:00]]
   (127.3ms)  commit transaction
Redirected to http://localhost:3000/users/6

Removing the ! gets the email to save but still not getting a passing test. It's my understanding that rspec creates new data each time you test, so it should be creating a new user with id 1 each time. User sessions are being created just fine now.

Processing by UserSessionsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"YGmKt135ndHaBhz03LaLZ5gU2280lwYQ9hSrJL/zHGA=", "email"=>"sick@email.com", "password"=>"[FILTERED]", "remember_me"=>"1", "commit"=>"Log In"}
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."email" = 'sick@email.com' LIMIT 1
Redirected to http://localhost:3000/todo_lists
Completed 302 Found in 54ms (ActiveRecord: 0.2ms)

Must be something in the test now. I've checked for typos and errors, but looks good. I'm out of time today. Update me if you find anything.

Graham Grochowski
Graham Grochowski
4,676 Points

thank you so much, explains why I couldn't figure out what was going wrong, I was thinking you could avoid using a = in that step by just doing self.email.downcase! and didn't take it out when I followed what the instructor did