[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Hashed_Pasword?

Jason James

2/21/2009 8:25:00 PM

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!
--
Posted via http://www.ruby-....

7 Answers

Tim Hunter

2/21/2009 8:38:00 PM

0

Jason James wrote:
> 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!

Is the variable named hashed_password or hashed_passwd?

--
RMagick: http://rmagick.ruby...

Jason James

2/21/2009 8:42:00 PM

0

Tim Hunter wrote:
> Jason James 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

--
Posted via http://www.ruby-....

Jason James

2/21/2009 9:17:00 PM

0

Jason James wrote:
> Tim Hunter wrote:
>> Jason James 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.
--
Posted via http://www.ruby-....

Hassan Schroeder

2/22/2009 3:17:00 PM

0

On Sat, Feb 21, 2009 at 12:24 PM, Jason James <jasonjames15@mac.com> 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 :-)

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com

Michael Malone

2/22/2009 8:28:00 PM

0


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


Rick DeNatale

2/23/2009 12:24:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

On Sun, Feb 22, 2009 at 3:27 PM, Michael Malone
<michael.malone@tait.co.nz>wrote:

>
> 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
>>>
>>>
>>
>>
>>
> 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?
>
>
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 Schroeder 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.denh...
Twitter: http://twitter.com/Ri...
WWR: http://www.workingwithrails.com/person/9021-ric...
LinkedIn: http://www.linkedin.com/in/ri...

Jason James

2/23/2009 4:41:00 PM

0

Rick Denatale wrote:
> On Sun, Feb 22, 2009 at 3:27 PM, Michael Malone
> <michael.malone@tait.co.nz>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 Schroeder 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.denh...
> Twitter: http://twitter.com/Ri...
> WWR: http://www.workingwithrails.com/person/9021-ric...
> LinkedIn: http://www.linkedin.com/in/ri...
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!
--
Posted via http://www.ruby-....