本文介绍了加载ActiveModel :: SecurePassword未定义的方法`password_digest ='的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试按照使用轨道3.1加载ActiveModel :: SecurePassword的

和我结束了红灯......

user.rb

 类用户的LT;的ActiveRecord :: Base的
  has_secure_password
  验证:密码:presence => {:上=> :创建 }
结束
 

factory.rb

  Factory.define:用户不要| F |
  [email protected]
  f.passwordFOOBAR
  f.password_confirmation {| U | u.password}
结束
 

spec_user.rb

 描述用户做
  它应该相匹配的用户名和密码进行身份验证吗
    用户=厂(:用户:电子邮件=>[email protected]',:密码=>秘密)
    User.authenticate('[email protected]','秘密')。应==用户
  结束
结束
 

和我得到了红灯......

 故障/错误:用户=厂(:用户:电子邮件=>[email protected]',:密码=>秘密)
 NoMethodError:
   未定义的方法`password_digest ='为#<使用者:0xb383460>
 

和我认为这是耙分贝:迁移的问题,我看看铁轨C,但明显password_digest定义

 红宝石-1.9.2-P180:007> A = User.new
 => #<使用者ID:无,电子邮件:无,password_digest:无,is_admin:无,created_at:无,的updated_at:无>
红宝石-1.9.2-P180:008> a.password_digest = 3
 => 3
 

解决方案

我得到了同样的问题,并找到了(我认为是)以下注释更好的解决方案:

http://bcardarella.com/post/4668842452/exploring-rails-3-1-activemodel-securepassword#comment-281584959

基本上,你需要一个password_digest字段添加到您的模型迁移。在此之前,它会增加一个password_digest =方法,但它不会被保存,并且该方法won'tr出现在工厂之类的

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 
解决方案

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

这篇关于加载ActiveModel :: SecurePassword未定义的方法`password_digest ='的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 06:04