Please help me decipher an error message


#1

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:inpassword=’
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


#2

replace

MD5.md5(‘password’).hexdigest

with

Digest::MD5.hexdigest(‘password’)


#3

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.