heroxav
heroxav

Reputation: 1467

Rails, Devise: unknown attribute 'password' for User

I have been using the devise-two-factor gem on my devise user model. When I try to remove the gem from the model, I get the following error when creating a user record:

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


Here is my previous devise integration in my model:

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]

And my current integration:

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

My seed:

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!

How does deleting these two lines affect the password attribute of the model? That code snippet is the only thing I've changed.

Upvotes: 2

Views: 2116

Answers (2)

speedracr
speedracr

Reputation: 395

two_factor_authenticatable is the explanation: Devise can only work with passwords if your model has some form of :authenticatable. Try adding :database_authenticatable instead and Devise should be able to assign the password correctly.

If that doesn't help, see the Devise documentation for more: https://github.com/plataformatec/devise/wiki/OmniAuth%3A-Overview#using-omniauth-without-other-authentications

Upvotes: 3

ArtOfCode
ArtOfCode

Reputation: 5712

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

There's your problem. You're trying to set a password attribute on a new User, but the Devise User model doesn't have a password attribute.

Devise users have an encrypted_password attribute, but that gets generated by Devise when a user is created and you probably shouldn't try to fill it on your own. It's not a method that encrypts a password attribute.

Upvotes: 2

Related Questions