Ruby On Rails Classroom image

Anil  Bist / Professional / Web Technology

 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|1 | 2|
Lessons:- Password and Confirmation

In the present case, we use it to set both password attributes to " ":


@user   .password  =  @user .password confirmation =  " "

 

We also want to ensure that the password and confirmation match. The case where they do match is already covered by it { should be_valid }, so we only need to test the case of a mismatch: 

describe   "when password doesn't match confirmation"  do
before  { @ user.password  confirmation =  "mismatch"  }
it { should not be valid }
end

 

In principle, we are now done, but there is one case that doesn’t quite work. What if the password confirmation is blank? If it is empty or consist of whitespace but the password is valid, then the two don’t match and the confirmation validation will catch it. If both the password and its confirmation areempty or consist of whitespace, then the password presence validation will catch it. Unfortunately, there’s one more possibility, which is that the password confirmation is nil. This can never happen through the web, but it can at the console:

 

$  rails  console
>>  User .create ( name:  "Michael Hartl",  email:   "mhartl@example.com",
                         ?>  password:   "foobar" password confirmation:   nil

 

When the confirmation is nil, Rails doesn’t run the confirmation validation, which means that we can create users at the console without password confirmations. (Of course, right now we haven’t added the validations yet, so the code above will work in any case.) To prevent this, we’ll add a test to catch this case:

 

describe   "when password confirmation is nil"  do
                before {  @user .password  confirmation =  nil }
              it { should not be valid }
end

 

(This behavior strikes me as a minor bug in Rails, and perhaps it will be fixed in a future version, and in any case adding the validation does no harm.)

               Putting everything together gives the (failing) tests in Listing 6.28. We’ll get them to pass in Section 6.3.4.

 

 
 
 
image
Anil  Bist

Skills    Ruby On Rails

Qualifications :- High School - SLV, College/University - Graphic Era Deemed Univ University,
Location :-Dehradun,Dehradun,Uttarakhand,India
Description:-

I started my Professional Journey in 2006 with one of the Web Development Company in Bangalore and my 1st framework was "Ruby on Rail" as Web development and delivered around 5+ Projects using this platform. Then came another dimension as JEE/Sturst framework, Gradually I realized that I want to build something on my own and give my passion and energy on creating something different a
Explore

 

  Students (0)