# Hashed\_Pasword?

**URL:** <https://www.ruby-forum.com/t/hashed-pasword/160986>\
**Category:** Ruby\
**Created:** [February 21, 2009, 9:25pm UTC](https://www.ruby-forum.com/t/hashed-pasword/160986 "2009-02-21T21:25:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jung](https://www.ruby-forum.com/u/jung)\
**Post date:** [February 21, 2009, 9:25pm UTC](https://www.ruby-forum.com/t/hashed-pasword/160986/1 "2009-02-21T21:25:24Z")

</div>

I am having a problem with my hashed\_password.  
I am following the lessons in the apress beginning rails book.  
Here is my issue:

def password\_required?  
hashed\_password.blank? || !password.blank?  
end

Ruby is calling this method out as a problem.

Error message from the web server:  
undefined local variable or method `hashed\_password’ for #\<User id: nil,  
login: “”, email: “”, hashed\_passwd: nil\>

I am stuck!

---

<div class="post-metadata">

**Author:** ![jung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jung](https://www.ruby-forum.com/u/jung)\
**Post date:** [February 21, 2009, 9:39pm UTC](https://www.ruby-forum.com/t/hashed-pasword/160986/2 "2009-02-21T21:39:00Z")

</div>

Jason J. wrote:

> Error message from the web server:  
> undefined local variable or method `hashed\_password’ for #\<User id: nil,  
> login: “”, email: “”, hashed\_passwd: nil\>
> 
> I am stuck!

Is the variable named hashed\_password or hashed\_passwd?

---

<div class="post-metadata">

**Author:** ![jung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jung](https://www.ruby-forum.com/u/jung)\
**Post date:** [February 21, 2009, 9:42pm UTC](https://www.ruby-forum.com/t/hashed-pasword/160986/3 "2009-02-21T21:42:01Z")

</div>

Tim H. wrote:

> Jason J. wrote:
> 
> > Error message from the web server:  
> > undefined local variable or method `hashed\_password’ for #\<User id: nil,  
> > login: “”, email: “”, hashed\_passwd: nil\>
> > 
> > I am stuck!
> 
> Is the variable named hashed\_password or hashed\_passwd?

Here is the entire section of code :  
def self.authenticate(login, password)  
user = find\_by\_login(login)  
return user if user && user.authenticated?(password)  
end

def authenticated?(password)  
hashed\_password == encrypt(password)  
end

protected  
def encrypt\_new\_password  
return if password.blank?  
self.hashed\_password = encrypt(password)  
end

def password\_required?  
hashed\_password.blank? || !password.blank?  
end

def encrypt(string)  
Digest::SHA1.hexdigest(string)  
end

---

<div class="post-metadata">

**Author:** ![jung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jung](https://www.ruby-forum.com/u/jung)\
**Post date:** [February 22, 2009, 4:18pm UTC](https://www.ruby-forum.com/t/hashed-pasword/160986/4 "2009-02-22T16:18:01Z")

</div>

On Sat, Feb 21, 2009 at 12:24 PM, Jason J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Error message from the web server:  
> undefined local variable or method `hashed\_password’ for #\<User id: nil,  
> login: “”, email: “”, hashed\_passwd: nil\>

It appears your model has a field called ‘hashed\_passwd’, which is  
not the same as ‘hashed\_password’. So change one of 'em 🙂

---

<div class="post-metadata">

**Author:** ![jung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jung](https://www.ruby-forum.com/u/jung)\
**Post date:** [February 21, 2009, 10:17pm UTC](https://www.ruby-forum.com/t/hashed-pasword/160986/5 "2009-02-21T22:17:53Z")

</div>

Jason J. wrote:

> Tim H. wrote:
> 
> > Jason J. wrote:
> > 
> > > Error message from the web server:  
> > > undefined local variable or method `hashed\_password’ for #\<User id: nil,  
> > > login: “”, email: “”, hashed\_passwd: nil\>
> > > 
> > > I am stuck!
> > 
> > Is the variable named hashed\_password or hashed\_passwd?
> 
> Here is the entire section of code :  
> def self.authenticate(login, password)  
> user = find\_by\_login(login)  
> return user if user && user.authenticated?(password)  
> end
> 
> def authenticated?(password)  
> hashed\_password == encrypt(password)  
> end
> 
> protected  
> def encrypt\_new\_password  
> return if password.blank?  
> self.hashed\_password = encrypt(password)  
> end
> 
> def password\_required?  
> hashed\_password.blank? || !password.blank?  
> end
> 
> def encrypt(string)  
> Digest::SHA1.hexdigest(string)  
> end

The web server is calling out line 47 which is hashed\_password.blank? ||  
!password.blank?

I think the hashed\_password.blank? is my problem but I do not know why.

the variable is hashed\_password.

---

<div class="post-metadata">

**Author:** ![jung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jung](https://www.ruby-forum.com/u/jung)\
**Post date:** [February 22, 2009, 9:29pm UTC](https://www.ruby-forum.com/t/hashed-pasword/160986/6 "2009-02-22T21:29:08Z")

</div>

> > protected  
> > Digest::SHA1.hexdigest(string)  
> > end

Um, shouldn’t you be storing the result of the hash into  
hashed\_password?  
Also, if this is part of a User class (as suggested by the output  
earlier) then you probably want to use @ -scoped variables, not  
method-local.  
def Klass  
some\_variable = value  
end

is NOT the same as:  
def Klass  
def initialize  
@some\_variable = value  
end  
end

That seems the most likely problem to me. If this doesn’t solve your  
problem, could you post the point at which you initialise/declare your  
variables?

Cheers,  
Michael

# ======================================================================= This email, including any attachments, is only for the intended addressee. It is subject to copyright, is confidential and may be the subject of legal or other privilege, none of which is waived or lost by reason of this transmission. If the receiver is not the intended addressee, please accept our apologies, notify us by return, delete all copies and perform no other act on the email. Unfortunately, we cannot warrant that the email has not been altered or corrupted during transmission.

---

<div class="post-metadata">

**Author:** ![jung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jung](https://www.ruby-forum.com/u/jung)\
**Post date:** [February 23, 2009, 1:25am UTC](https://www.ruby-forum.com/t/hashed-pasword/160986/7 "2009-02-23T01:25:27Z")

</div>

On Sun, Feb 22, 2009 at 3:27 PM, Michael M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > > def encrypt(string)  
> > > def Klass  
> > > That seems the most likely problem to me. If this doesn’t solve your  
> > > problem, could you post the point at which you initialise/declare your  
> > > variables?

No, you should be aware that this is pretty obviously an ActiveRecord  
model  
object in a Rails app. The accessor methods for database fields are  
generated automagically from the database schema.

It’s also probably using either the restful\_authentication plugin, or  
it’s  
older brother acts\_as\_authenticated, both of which normally use  
crypted\_password, instead of hashed\_password for the field name,  
although I  
beleive that this can be overridden when the authentication code is  
generated.

I’m pretty sure that Hassan S. has the right diagnosis.  
undefined local variable or method `hashed\_password’ for #\<User id: nil,  
login: “”, email: “”, hashed\_passwd: nil\>

This indicates that the database column is named hashed\_passwd NOT  
hashed\_password, so ActiveRecord isn’t providing a method with the name  
hashed\_password.

This is the kind of question which would probably be answered much more  
quickly, and with less confusion on the rails mailing list.

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![jung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jung](https://www.ruby-forum.com/u/jung)\
**Post date:** [February 23, 2009, 5:41pm UTC](https://www.ruby-forum.com/t/hashed-pasword/160986/8 "2009-02-23T17:41:17Z")

</div>

Rick Denatale wrote:

> On Sun, Feb 22, 2009 at 3:27 PM, Michael M.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:
> 
> > > > def encrypt(string)  
> > > > def Klass  
> > > > That seems the most likely problem to me. If this doesn’t solve your  
> > > > problem, could you post the point at which you initialise/declare your  
> > > > variables?
> 
> No, you should be aware that this is pretty obviously an ActiveRecord  
> model  
> object in a Rails app. The accessor methods for database fields are  
> generated automagically from the database schema.
> 
> It’s also probably using either the restful\_authentication plugin, or  
> it’s  
> older brother acts\_as\_authenticated, both of which normally use  
> crypted\_password, instead of hashed\_password for the field name,  
> although I  
> beleive that this can be overridden when the authentication code is  
> generated.
> 
> I’m pretty sure that Hassan S. has the right diagnosis.  
> undefined local variable or method `hashed\_password’ for #\<User id: nil,  
> login: “”, email: “”, hashed\_passwd: nil\>
> 
> This indicates that the database column is named hashed\_passwd NOT  
> hashed\_password, so ActiveRecord isn’t providing a method with the name  
> hashed\_password.
> 
> This is the kind of question which would probably be answered much more  
> quickly, and with less confusion on the rails mailing list.
> 
> –  
> Rick DeNatale
> 
> Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
> Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
> WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
> LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)  
> Rick,  
> Thanks for the resources. I will use the mailing list next time.  
> You were right - I figured out before seeing your post. I submitted the  
> new user request and was able to look at the error messages from the  
> web server which led me to take a second look at the database.

Sure enough there was a field spelling error as you pointed out.  
Thanks again!
