gpt4 book ai didi

ruby-on-rails - ActiveModel::SecurePassword 未定义方法 `password_digest='

转载 作者:行者123 更新时间:2023-12-04 06:23:23 24 4
gpt4 key购买 nike

我尝试按照http://bcardarella.com/post/4668842452/exploring-rails-3-1-activemodel-securepassword使用rails 3.1 ActiveModel::SecurePassword

最后我遇到了红灯......

用户.rb

class User < ActiveRecord::Base
has_secure_password
validates :password, :presence => { :on => :create }
end

工厂.rb

Factory.define :user do |f|
f.email "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6c0a03032c0e0d1e420f0301" rel="noreferrer noopener nofollow">[email protected]</a>"
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 => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cfa9bdaea1a48fa8a2aea6a3e1aca0a2" rel="noreferrer noopener nofollow">[email protected]</a>', :password => 'secret')
User.authenticate('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3a5c485b54517a5d575b535614595557" rel="noreferrer noopener nofollow">[email protected]</a>', 'secret').should == user
end
end

我遇到了红灯...

 Failure/Error: user = Factory(:user, :email => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0c6a7e6d62674c6b616d6560226f6361" rel="noreferrer noopener nofollow">[email protected]</a>', :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

最佳答案

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

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

基本上,您需要通过迁移向模型添加一个password_digest 字段。在此之前,它会添加一个password_digest=方法,但不会保存,并且该方法不会出现在工厂等中

关于ruby-on-rails - ActiveModel::SecurePassword 未定义方法 `password_digest=',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6069304/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com