# Cannot get my password to validate

**URL:** <https://www.ruby-forum.com/t/cannot-get-my-password-to-validate/175381>\
**Category:** Rails\
**Created:** [September 12, 2009, 10:23am UTC](https://www.ruby-forum.com/t/cannot-get-my-password-to-validate/175381 "2009-09-12T10:23:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RubyonRails\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@RubyonRails\_newbie](https://www.ruby-forum.com/u/RubyonRails_newbie)\
**Post date:** [September 12, 2009, 10:23am UTC](https://www.ruby-forum.com/t/cannot-get-my-password-to-validate/175381/1 "2009-09-12T10:23:44Z")

</div>

Hello,

I am working through the railsspace tutorrial, and i’ve noticed that  
the password validation is no longer working.

I can get screen\_name and email to validate, but for some reason, the  
password field is ignored…

here’s what i have so far:

within user.rb file  
PASSWORD\_MIN\_LENGTH = 4  
PASSWORD\_MAX\_LENGTH = 40

validates\_length\_of :password, :within =\> PASSWORD\_RANGE

def validate  
errors.add(:email, “must be valid.”) unless email.include?("@")  
if screen\_name.include?(" “)  
errors.add(:screen\_name, “cannot include spaces.”)  
if password.include?(” ")  
errors.add(:password, “Must be filled in”)  
end

```
end

```

In my user controller, i have a digest to mask the password in the  
database at registration:

def register

```
 @title = "Register"
 if request.post? and params[:user]
     @user = User.new(params[:user])
       @user.password = Digest::SHA1.hexdigest

```

(@user.password)  
if @user.save

```
        session[:user_id] = @user.id
        flash[:notice] = "User '#{@user.screen_name}' created!"
        redirect_to :action => "index", :controller => "user"
      else @user.password = nil

      end

 end

```

end

If I remove the Digest line, the password validation works again, but  
this means any password is then visible. I moved the digest line so it  
appeared AFTER the user.save.  
This solved the validation issue, but displayed the password in the  
database,

How do i get round this??

many thanks

---

<div class="post-metadata">

**Author:** ![RubyonRails\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@RubyonRails\_newbie](https://www.ruby-forum.com/u/RubyonRails_newbie)\
**Post date:** [September 12, 2009, 10:54am UTC](https://www.ruby-forum.com/t/cannot-get-my-password-to-validate/175381/2 "2009-09-12T10:54:37Z")

</div>

Hi  
You can solve it like

```
        @user.password = Digest::SHA1.hexdigest(@user.password) if 

```

@user.valid?

Change only the above line .  
What happens in your code is empty string also gets digested So  
fails validation.

Sijo

---

<div class="post-metadata">

**Author:** ![RubyonRails\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@RubyonRails\_newbie](https://www.ruby-forum.com/u/RubyonRails_newbie)\
**Post date:** [September 12, 2009, 10:56am UTC](https://www.ruby-forum.com/t/cannot-get-my-password-to-validate/175381/3 "2009-09-12T10:56:30Z")

</div>

Hello!  
Try to look at authlogic plugin for user authentication:

> **[GitHub - binarylogic/authlogic: A simple ruby authentication solution.](https://github.com/binarylogic/authlogic/tree/master)**
>
> master

Its really cool!

> if password.include?(" ")  
> errors.add(:password, “Must be filled in”)  
> why??

for saving crypted password in database use before\_create callback

class User \< AR::B  
before\_cerate :crypt\_password

protected

def crypt\_password  
self.password = Digest::SHA1.hexdigest(password)  
end  
end

On 12 ÓÅÎ, 12:21, RubyonRails\_newbie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![RubyonRails\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@RubyonRails\_newbie](https://www.ruby-forum.com/u/RubyonRails_newbie)\
**Post date:** [September 12, 2009, 8:46pm UTC](https://www.ruby-forum.com/t/cannot-get-my-password-to-validate/175381/4 "2009-09-12T20:46:25Z")

</div>

Thanks!!

```
     @user.password = Digest::SHA1.hexdigest(@user.password) if

```

> @user.valid?

works perfectly!

thanks a lot!!
