Agile book - getting confusing error


#1

Working through the beginning phase from the Agile book on
‘Administration’

undefined method hashed_password=' for #<User:0xb7911324> ... /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1498:inmethod_missing’
#{RAILS_ROOT}/app/models/user.rb:12:in before_create' ./script/../config/../app/controllers/login_controller.rb:8:inadd_user’

Request
Parameters: {“user”=>{“name”=>“Craig W.”, “login”=>“craig”,
“password”=>“test”}}

OK…

My user.rb includes…

require “digest/sha1”
class User < ActiveRecord::Base

attr_accessor :password
attr_accessible :login, :password, :name

def before_create
self.hashed_password = User.hash_password(self.password)
end

private

def self.hash_password(password)
Digest::SHA1.hexdigest(password)
end
end

Where am I going wrong?

Craig


#2

Try this in your before_create() method:

@hashed_password = User.hash_password(password)

This will create an instance variable for this User, named
@hashed_password and populated with the SHA digested value of the
password that was entered in the request.

It looks to me like you are using “self” in Ruby as you would use
“this” in Java, which is going to cause problems like this. They’re
not equivalent.

Hope this helps,
David


#3

That get’s me past the error but then the password column is written as
null to the db which obviously isn’t the expected behavior. I believe
the intent is to take the :password and hash it just prior to writing it
to db so putting the hash into an instance variable would require
another assignment back…which I will play with but it seems like an
extra step which I think was the author’s intention of using ‘self’.

I am taking this out of the book so I expected it to work.

Thanks

Craig


#4

On Wed, 2006-02-15 at 18:44 +0100, joey__ wrote:

end
Craig

Try remove the ‘private’, see what happens.


I tried that before I posted to list…didn’t change anything.

I have tried a lot of stuff - getting stuck following a book is
frustrating.

Thanks

Craig


#5

Craig W. wrote:

require “digest/sha1”
class User < ActiveRecord::Base

attr_accessor :password
attr_accessible :login, :password, :name

def before_create
self.hashed_password = User.hash_password(self.password)
end

private

def self.hash_password(password)
Digest::SHA1.hexdigest(password)
end
end

Where am I going wrong?

Craig

Try remove the ‘private’, see what happens.

Joey


#6

I am so dumb…thanks

column was called ‘password’ and not ‘hashed_password’

thanks

Craig


#7

Hmm. Does your users table have a column named “hashed_password”?
Your model should automatically know about all the columns in its
corresponding table (User -> users, in your case).

If your users table doesn’t have this column, you should add it and
restart your server. If it does have the column, you may still need
to restart your server (to pick up the most recent version of the
database).

David