问题
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
回答1:
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.
回答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.
回答3:
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
来源:https://stackoverflow.com/questions/55626625/devise-token-auth-conflicts