Devise_token_auth conflicts?
Asked Answered
C

3

5

Made a new API in rails 5 using default --api tag and installed devise_token_auth gem using command rails generate devise_token_auth:install User auth . On doing rake:db:migrate, I get this error undefined method 'devise' for User (call 'User.connection' to establish a connection) which is weird because devise_token_auth is built on top of devise..

So, commenting out routes throws error to user.rb file containing

devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable,:omniauthable include DeviseTokenAuth::Concerns::User

After commenting those lines out it throws error to application controller containing: include DeviseTokenAuth::Concerns::SetUserByToken After commenting out this line, I get the devise:orm error.

Also tried adding devise to gemfile and installing devise for users, which failed too, throwing this error again undefined method 'devise' for User (call 'User.connection' to establish a connection)

Expected Migrations to go through,

Getting Devise errors

What should be done to resolve this?

PS- This was a third API I'm making using Devise_token_auth gem, didn't face this issue earlier

Captor answered 11/4, 2019 at 7:10 Comment(0)
P
7

This is a devise Issue, ActiveRecord ORM was hard coded inside of the gem before which has been changed now. This can be resolved by creating and adding the devise initializer as mentioned in the below documentation.

https://devise-token-auth.gitbook.io/devise-token-auth/config/initialization

There is also another way of fixing this, by adding the following in your user.rb model file.

extend Devise::Models

There is also an open issue in devise_token_auth

https://github.com/lynndylanhurley/devise_token_auth/issues/1276

Where you can follow up regarding this issue / Add in your thoughts for resolving this.

Parsec answered 11/4, 2019 at 9:43 Comment(0)
R
2

Did you do the following steps -

Include gem in the gemfile-

gem 'devise_token_auth'

run generator:

rails generate devise_token_auth:install User auth

check if below line is added in User model:

include DeviseTokenAuth::Concerns::User

check if below line is added in Application controller:

include DeviseTokenAuth::Concerns::SetUserByToken

check if below line is added in routes.rb:

mount_devise_token_auth_for 'User', at: 'auth'

add extend Devise::Models in User model

run rake db:migrate

Can you try removing the devise ,devise_token_auth gem and try these steps? Let me know if any error pops up.

Rebarebah answered 11/4, 2019 at 9:5 Comment(3)
I did exactly the same already!! And tried it on a new App :- Error on rake db:migrate :- NoMethodError: undefined method devise' for User (call 'User.connection' to establish a connection):Class`Captor
@AmanRelan can you add extend Devise::Models in the User model and check the same?Rebarebah
That's what worked! thanks to you too for your help!Captor
W
1

I got this error and this was a fix for me.

1) Add 'devise_token_auth' gem in the Gemfile.

2) run rails g devise:install

3) run rails generate devise_token_auth:install User auth

4) run rails db:migrate

Wimer answered 4/8, 2019 at 8:53 Comment(1)
This fixed the issue right on this pull: github.com/lynndylanhurley/devise_token_auth/pull/1307Captor

© 2022 - 2024 — McMap. All rights reserved.