Forum: Ruby on Rails Please help me decipher an error message

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.
William L. (Guest)
on 2007-07-17 09:58
Hello,
    I am adding a basic authentication scheme to my application. When I
go to the console and try to add a new User I'm getting an error which I
don't know what to do with. Any help would be wonderful.

Here's my console session:

>> u = User.new
=> #<User:0x32f8aac @attributes={"username"=>nil, "password"=>nil},
@new_record=true>
>> u.password = 'test'
NameError: uninitialized constant User::MD5
        from
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:477:in
`const_missing'
        from ./script/../config/../config/../app/models/user.rb:16:in
`password='
        from (irb):2
>>


Here's my User model:

class User < ActiveRecord::Base

  require 'digest/md5'

  def self.authenticate(name, password)
    user = User.find(:first,
                      :conditions => ["name = ? AND password = ?",
                      name, MD5.md5(password).hexdigest])
    if user.blank?
      raise "Name or password is invalid"
    end
    user
  end

  def password=(pswd)
    self.password = MD5.md5(pswd).hexdigest
  end

end

I think it's not finding the digest/md5 file. If that's the case I'm not
really sure how to remedy that.

Thanks,
    William
Chris H. (Guest)
on 2007-07-19 03:11
(Received via mailing list)
replace

MD5.md5('password').hexdigest

with

Digest::MD5.hexdigest('password')
William L. (Guest)
on 2007-07-19 10:50
Chris H. wrote:
> replace
>
> MD5.md5('password').hexdigest
>
> with
>
> Digest::MD5.hexdigest('password')

Thank you. That did the trick.

I'm not sure why the syntax changed. I copied
'MD5.md5('password').hexdigest, from a test site I did over  a year ago
which worked just fine.
This topic is locked and can not be replied to.