ActiveModel::SecurePassword 未定义方法 `password_digest='

发布于 2024-11-08 18:55:49 字数 1863 浏览 0 评论 0原文

我尝试按照 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

久伴你 2024-11-15 18:55:49

我遇到了同样的问题,并在以下评论中找到了(我认为是)更好的解决方案:

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

猫七 2024-11-15 18:55:49

解决了

describe User do
  it "should authenticate with matching username and password" do
    user = Factory(:user, :email => '[email protected]', :password => 'secret')
    User.find_by_email('[email protected]').try(:authenticate, 'secret').should == user
  end
end

solved by

describe User do
  it "should authenticate with matching username and password" do
    user = Factory(:user, :email => '[email protected]', :password => 'secret')
    User.find_by_email('[email protected]').try(:authenticate, 'secret').should == user
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文