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

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.


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

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

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

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

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


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

    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

Maciej Czuchnowski
Maciej Czuchnowski
36,441 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.