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 Using has_secure_password

gerald blady
gerald blady
9,052 Points

downcase email fails

I have no idea how this is failing? another set of eyes would be great.

Failures:

1) User#downcase_email downcases an email before saving Failure/Error: expect(user.save).to be_true expected: truthy value got: false # ./spec/models/user_spec.rb:53:in `block (3 levels) in <top (required)>' require 'spec_helper'

describe User do let(:valid_attributes){ { first_name: "Jerry", last_name: "Blady", email: "jerry@gmail.com", password: "treehouse1234", password_confirmation: "treehouse1234" } } context "validations" do let(:user) { User.new(valid_attributes) }

before do
    User.create(valid_attributes)
end

it "requires an email" do 
    expect(user).to validate_presence_of(:email)
end

it "requires a unique email" do  
    expect(user).to validate_uniqueness_of(:email)
end

it "requires a unique email case insensitive" do 
    user.email = "MIKE@GMAIL.COM"
    expect(user).to validate_uniqueness_of(:email)
end     

it "requires email address to look like an email address" do 
    user.email = "jason"
    expect(user).to_not be_valid
end

end

context "#downcase_email" do  
    it "makes the email attribute lower case" do
        user = User.new(valid_attributes.merge(email: "JERRY@GMAIL.COM"))
        expect{ user.downcase_email }.to change { user.email }.
            from("JERRY@GMAIL.COM").
            to("jerry@gmail.com")
    end

    it "downcases an email before saving" do            
    user = User.new(valid_attributes)
    user.email = "MIKE@GMAIL.COM"
    expect(user.save).to be_true
    expect(user.email).to eq("mike@gmail.com")

end

end end

Maciej Czuchnowski
Maciej Czuchnowski
36,440 Points

We can't help you if we don't see the whole code. There's got to be some small bit you overlooked while doing the project.

1 Answer

Thomas Johns
Thomas Johns
12,081 Points

I got the same error. If you create a user in the console (as u = User.create(first_name: "test"...etc)) with an email you can see it fail. If you then do u.errors you will most likely get an "Invalid Email". Jason's expression is the culprit for some reason.

Replace your format with this:

validates :email, presence: true,
                uniqueness: true,
                format: {
                  with: /\A[^@\s]+@([^@.\s]+\.)+[^@.\s]+\z/
                }

or another one from here: http://stackoverflow.com/questions/4770133/rails-regex-for-email-validation

and it will work. Be sure to reload! if you are testing in the console.