Rails,Devise:用户的未知属性'密码'

问题描述:

我一直在使用我的devise用户模型上的devise-two-factor宝石。当我尝试从模型中移除宝石,我创建一个用户记录时收到以下错误:Rails,Devise:用户的未知属性'密码'

ActiveModel::UnknownAttributeError: unknown attribute 'password' for User. c:/test_app/db/seeds.rb:6:in <top (required)>' bin/rails:4:in require' bin/rails:4:in `'


这里是我以前的色器件集成在我的模型:

devise :registerable, 
     :recoverable, :rememberable, :trackable, :validatable, :confirmable, 
     :omniauthable, 
     :invitable, 
     :two_factor_authenticatable, :two_factor_backupable, 
     :otp_secret_encryption_key => Settings.devise.two_factor.key, 
     :omniauth_providers => [:google_oauth2] 

而且我目前的积分:

devise :registerable, 
     :recoverable, :rememberable, :trackable, :validatable, :confirmable, 
     :omniauthable, 
     :invitable, 
     :omniauth_providers => [:google_oauth2] 

我的种子:

demo_user = User.create(email: '[email protected]', first_name: 'demo', last_name: 'account', password: '12345678', username: 'demo') 
demo_user.encrypted_password 
demo_user.skip_confirmation! 

如何删除这两条线影响模型的password属性?该代码片段是我改变的唯一的东西。

+2

您可以将您的'DB/seeds.rb'文件的问题? – ArtOfCode

+0

@ArtOfCode我将它添加到标记 – jonhue

two_factor_authenticatable是解释:设计只能使用密码,如果您的模型有某种形式:authenticatable。 尝试添加:database_authenticatable,而Devise应该能够正确地分配密码。

如果没有帮助,请参阅更多的设计文档:https://github.com/plataformatec/devise/wiki/OmniAuth%3A-Overview#using-omniauth-without-other-authentications

demo_user = User.create(email: '[email protected]', first_name: 'demo', 
         last_name: 'account', password: '12345678', 
         username: 'demo') 

这是您的问题。您正尝试在新的User上设置password属性,但设计User型号没有password属性。

设计用户拥有encrypted_password属性,但是在创建用户时由Devise生成,您可能不应该尝试自行填充它。这不是一种加密password属性的方法。

+0

我不认为这是打算,因为我的设计注册形式与默认的'密码'输入也失败。 – jonhue

+0

我仍然不知道为什么'password'取决于'two-factor'。随着宝石一切正常。 – jonhue

+0

'User.create(email:'...',密码:'12341234',password_confirmation:'12341234')'会用Devise保存一个加密的密码。 – speedracr