ActiveModel :: SecurePassword未定义方法`password_digest =’
我尝试按照http://bcardarella.com/post/4668842452/exploring-rails-3-1-activemodel-securepassword使用rails 3.1 ActiveModel :: SecurePassword
我最后得到了红灯……
user.rb
class User { :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 #
我认为这是rake db:migrate问题,我查看了rails c,但显然定义了password_digest。
ruby-1.9.2-p180 :007 > a = User.new => # ruby-1.9.2-p180 :008 > a.password_digest = 3 => 3
我遇到了同样的问题,在以下评论中找到了一个(我认为是)更好的解决方案:
基本上,您需要通过迁移向您的模型添加password_digest字段。 在此之前,它将添加一个password_digest =方法,但它不会被保存,并且该方法不会出现在工厂等中
解决了
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