Forum: Ruby on Rails Agile book - getting confusing error

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-02-15 18:32
(Received via mailing list)
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:in
`method_missing'
#{RAILS_ROOT}/app/models/user.rb:12:in `before_create'
./script/../config/../app/controllers/login_controller.rb:8:in
`add_user'
...
Request
Parameters: {"user"=>{"name"=>"Craig White", "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
6dab365a82517fb694650a57ee88e4a4?d=identicon&s=25 joey__ (Guest)
on 2006-02-15 18:44
Craig White 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
Bfc3ae865ab5cb781b9d1d4b50ab132b?d=identicon&s=25 David Rupp (Guest)
on 2006-02-15 18:53
(Received via mailing list)
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
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-02-15 19:44
(Received via mailing list)
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
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-02-15 19:47
(Received via mailing list)
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
Bfc3ae865ab5cb781b9d1d4b50ab132b?d=identicon&s=25 David Rupp (Guest)
on 2006-02-15 20:03
(Received via mailing list)
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
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-02-15 20:30
(Received via mailing list)
I am so dumb...thanks

column was called 'password' and not 'hashed_password'

thanks

Craig
This topic is locked and can not be replied to.