Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby User Authentication with Rails Password Hashing and Sign In Using has_secure_password

I keep getting a failure on the user count not equaling 1?

Here is the error:

1) Signing up allows a user to sign up for the site and creates the object in the db Failure/Error: expect(User.count).to eq(1)

   expected: 1
        got: 0

   (compared using ==)
 # ./spec/features/users/registration_spec.rb:18:in `block (2 levels) in <top (required)>'

Finished in 0.15175 seconds 1 example, 1 failure

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

As always, need to see as much as your code as possible to help :).

2 Answers

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

OK, got your bug. In the users controller you have to change strong parameters - attributes that are allowed to be sent through forms to the database. The last method should reflect the form:

    def user_params
      params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation)
    end

I know that the database has :password_digest as a field, but has_secure_password changes things. I think they did this in the video somewhere.

How I found out and what you should do in the future: run your app, open the browser and repeat all the steps to see what happens. In this case I was getting error saying that password is blank no matter what I did, this told me that strong parameters were wrong.

This is great! Thank you!

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Hmmm...but your code on github expects 0 in both places:

require "spec_helper"

describe "Signing up" do
  it "allows a user to sign up for the site and creates the object in the database" do
    expect(User.count).to eq(0)

    visit "/"
    expect(page).to have_content("Sign Up")
    click_link "Sign Up"

    fill_in "First Name", with: "Jason"
    fill_in "Last Name", with: "Seifer"
    fill_in "Email", with: "jason@teamtreehouse.com"
    fill_in "Password", with: "treehouse1234"
    fill_in "Password (again)", with: "treehouse1234"
    click_button "Sign Up"

    expect(User.count).to eq(0)
  end

end

Yes, good catch. I just hacked it and changed it from 1 to 0n so that it would pass. :)