Forum: Ruby on Rails Problem with the Authorization recipe

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.
Ana B. (Guest)
on 2006-04-21 08:57
I just got the rails recipes book and i couldnt be more happy, but
inside the book there are some details, mostly typos but this one its
one i cant fix, im following the Authorization recipe on page 128 i
create the table and the model, the problem comes when i try to assign a
password to a user in the console, here is the output:

>> justin = User.create(:username => "justin")
=> #<User:0x3c9cd38 @errors=#<ActiveRecord::Errors:0x3c9b730 @errors={},
@base=#<User:0x3c9cd38 ...>>, @attributes={"password_salt"=>nil,
"username"=>"justin", "id"=>4, "password_hash"=>nil}, @new_record=false>
>> justin.password = "secret"
NameError: uninitialized constant Digest::SHA2
from
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.0/lib/active_support/dependencies.rb:89:in
`const_missing'
from ./script/../config/../config/../app/models/user.rb:5:in `password='
from (irb):4
>>

and here are my model:

class User < ActiveRecord::Base
  validates_uniqueness_of :username
  def password=(pass)
    salt = [Array.new(6){rand(256).chr}.join].pack("m").chomp
    self.password_salt, self.password_hash = salt,
Digest::SHA256.hexidigest(pass + salt)
  end
end

Its clearly seems to be a problem of the SHA2 library but searching
around i cant find any reasonable info that helps to fix this problem,
hope someone can give me a hand with this :)
Steve R. (Guest)
on 2006-04-21 09:28
(Received via mailing list)
You required digest/sha2 right?
--
View this message in context:
http://www.nabble.com/Problem-with-the-Authorizati...
Sent from the RubyOnRails Users forum at Nabble.com.
Ana B. (Guest)
on 2006-04-21 09:46
Steve R. wrote:
> You required digest/sha2 right?
> --
> View this message in context:
> http://www.nabble.com/Problem-with-the-Authorizati...
> Sent from the RubyOnRails Users forum at Nabble.com.

ok, dumb mistakenow i required sha2: require 'digest/sha2'  but the
problems keep coming, now i get this when trying to assign a password:

>> negro3.password = "secret"
NoMethodError: undefined method `hexidigest' for Digest::SHA256:Class
 from ./script/../config/../config/../app/models/user.rb:6:in
`password='
 from (irb):3
>>

Im using windows/rails1.1/ruby1.8.4 even in the forums of the recipes
book i cant find a good answer to fix this.
David M. (Guest)
on 2006-04-21 09:52
(Received via mailing list)
Typo - it should be 'hexdigest', not 'hexidigest'
Ana B. (Guest)
on 2006-04-21 10:00
David M. wrote:
> Typo - it should be 'hexdigest', not 'hexidigest'

oh no¡ no no no, i have 3 hours working in how to fix this problem, all
was a simple thing, that typo.

Thanks so much for your help guys, really ;)
This topic is locked and can not be replied to.