ActiveModel::SecurePassword 未定义方法 `password_digest='
我尝试按照 http:// /bcardarella.com/post/4668842452/exploring-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 "[email protected]"
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 => '[email protected]', :password => 'secret')
User.authenticate('[email protected]', 'secret').should == user
end
end
,我亮起红灯...
Failure/Error: user = Factory(:user, :email => '[email protected]', :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
I try to use rails 3.1 ActiveModel::SecurePassword by following http://bcardarella.com/post/4668842452/exploring-rails-3-1-activemodel-securepassword
and I end up with red light ...
user.rb
class User < ActiveRecord::Base
has_secure_password
validates :password, :presence => { :on => :create }
end
factory.rb
Factory.define :user do |f|
f.email "[email protected]"
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 => '[email protected]', :password => 'secret')
User.authenticate('[email protected]', 'secret').should == user
end
end
and i get a red light ...
Failure/Error: user = Factory(:user, :email => '[email protected]', :password => 'secret')
NoMethodError:
undefined method `password_digest=' for #<User:0xb383460>
and I thought it was rake db:migrate problem and I look into rails c , but obviously password_digest is defined.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同样的问题,并在以下评论中找到了(我认为是)更好的解决方案:
http://bcardarella.com/post/4668842452/exploring-rails-3-1-activemodel-securepassword#comment-281584959
基本上,您需要在您的具有迁移的模型。在此之前,它会添加一个password_digest=方法,但不会被保存,并且该方法不会出现在工厂等中
I was getting the same issue, and found a (what I think is) a better solution in the following comment:
http://bcardarella.com/post/4668842452/exploring-rails-3-1-activemodel-securepassword#comment-281584959
basically, you need to add a password_digest field to your model with a migration. Before then, it'll add a password_digest= method, but it won't be saved, and the method won'tr show up in factories and the like
解决了
solved by