我尝试按照http://bcardarella.com/post/4668842452/exploring-rails-3-1-activemodel-securepassword
使用rails 3.1 ActiveModel :: SecurePassword我结束了红灯......
user.rb
class User < ActiveRecord::Base
has_secure_password
validates :password, :presence => { :on => :create }
end
factory.rb
Factory.define :user do |f|
f.email "foo@bar.com"
f.password "foobar"
f.password_confirmation { |u| u.password }
end
spec_user.rb
describe User do
it "should authenticate with matching username and password" do
user = Factory(:user, :email => 'frank@gmail.com', :password => 'secret')
User.authenticate('frank@gmail.com', 'secret').should == user
end
end
我得到了红灯......
Failure/Error: user = Factory(:user, :email => 'frank@gmail.com', :password => 'secret')
NoMethodError:
undefined method `password_digest=' for #<User:0xb383460>
我认为这是rake db:migrate问题,我查看了rails c,但很明显定义了password_digest。
ruby-1.9.2-p180 :007 > a = User.new
=> #<User id: nil, email: nil, password_digest: nil, is_admin: nil, created_at: nil, updated_at: nil>
ruby-1.9.2-p180 :008 > a.password_digest = 3
=> 3
答案 0 :(得分:6)
我遇到了同样的问题,并在以下评论中找到了一个(我认为是)更好的解决方案:
基本上,您需要通过迁移向您的模型添加password_digest字段。在此之前,它将添加一个password_digest =方法,但它不会被保存,并且该方法不会出现在工厂等中
答案 1 :(得分:1)
解决了
describe User do
it "should authenticate with matching username and password" do
user = Factory(:user, :email => 'frank@gmail.com', :password => 'secret')
User.find_by_email('frank@gmail.com').try(:authenticate, 'secret').should == user
end
end