devise_token_auth icon indicating copy to clipboard operation
devise_token_auth copied to clipboard

devise_token_auth 1.1.0 installation fails with "undefined method 'devise' for User"

Open drj17 opened this issue 5 years ago • 11 comments

When posting issues, please include the following information to speed up the troubleshooting process:

  • Version: 1.1.0
  • Rails Version: 5.2.3
  • Rails Stacktrace:
rake aborted!
NoMethodError: undefined method `devise' for User (call 'User.connection' to establish a connection):Class
/app/models/user.rb:8:in `include'
/app/models/user.rb:8:in `<class:User>'
/app/models/user.rb:3:in `<main>'
/config/routes.rb:2:in `block in <main>'
/config/routes.rb:1:in `<main>'
/config/environment.rb:5:in `<main>'
Tasks: TOP => db:migrate => db:load_config => environment
(See full trace by running task with --trace)

Given the current installation settings in the documentation, installation fails in a brand new rails project.

drj17 avatar Mar 31 '19 19:03 drj17

@drj17 You can add extend Devise::Models in User.rb Model. It fixed for me.

vinhkq avatar Apr 02 '19 06:04 vinhkq

This is Devise issue, but not the gem. And this is a Stack Overflow question.

ActiveRecord ORM was hard coded inside of the gem before. Now you must follow by Devise configuration.

  # ==> ORM configuration
  # Load and configure the ORM. Supports :active_record (default) and
  # :mongoid (bson_ext recommended) by default. Other ORMs may be
  # available as additional gems.
  require 'devise/orm/<%= options[:orm] %>'

Check also the gem doc.

dks17 avatar Apr 06 '19 19:04 dks17

I have met with the same issue in version 1.1.0, when I change the devise_token_auth to previous version 1.0.0, it works.

jeffjiang0613 avatar Apr 14 '19 01:04 jeffjiang0613

I would argue this is not a devise issue because you cannot install this gem with the readme as is. Perhaps someone would like to make that pull request? I could do it one I get access to a computer

drj17 avatar Apr 14 '19 01:04 drj17

I had the same issue, and just with 1.0.0 version works fine. If I use extend Devise::Models in User.rb raise an error with /auth/sign_in (creating sessions)

reneclavijo avatar Apr 17 '19 03:04 reneclavijo

The update to the latest version should be documented in a better way, the way to go is create an initializer for devise and load active record, like @drj17 said, the changelog of this gem does not mention the upgrade step, and does not mention the new devise dependency.

I think we should definitely put something in the installation documentation, or at least an 'upgrade' section in the gem docs.

CDimonaco avatar May 21 '19 14:05 CDimonaco

Any progress on this? I'm running into the same issue. @Tiltorito I see you opened a PR but the tests were failing. To me it looks like the reason was an intermittent issue with Travis. You could try restarting the build and that might fix it, but I could be way off base.

dkniffin avatar Oct 18 '19 20:10 dkniffin

Ran into the same issue. Things that didnt work for me:

  1. Adding extend Devise::Models in User.rb
  2. rails generate devise:install after getting the error.

What worked for me:

  1. Rollback any changes files/migrations made by the devise_token_auth. Get to a state where your app has never heard of this gem.
  2. Before doing anything with devise_token_auth, first install devise by adding to Gemfile and running bundle install
  3. Run rails generate devise:install as per devise's installation instructions
  4. This will generate 2 files: config/initializers/devise.rb and config/locales/devise.en.yml
  5. Now start with devise_token_auth instructions by adding to Gemfile/bundle install/running the generator (rails g devise_token_auth:install User auth)

If this is the best way to solve this problem, I'd be happy to create a PR for the docs, but sounds like there are a lot of ways to skin this cat and not all of them work for everyone

udit99 avatar Aug 18 '21 15:08 udit99

The approuch of @udit99 worked. Another workflow can be:

  1. comment Device code reference in models/user.rb and config/routes.rb
  2. run rails generate devise:install. It should generate the initialization that we need.
  3. Rollback the step 1.

I think should include the Device initialization in the documentation.

Thanks @udit99

Luiyit avatar Apr 15 '22 03:04 Luiyit

Thank you @udit99 and @Luiyit you're steps worked. So then, is it necessary to run rails generate devise:install before rails generate devise_token_auth install?

Or maybe that is not ideal because that will install a bunch of Devise stuff that is not needed for token auth? Just need a way to generate the config/initializers/devise.rb file, correct?

johnpitchko avatar Sep 03 '22 14:09 johnpitchko

Hi @johnpitchko. I think so. You can run devise_token_auth first, but you will get the error, for that reason in this case we need to comment model files (on my steps) or rollback migration files (on audit99's steps).

I think the correct step by stem should be (from scratch)

  1. Add device_token_auth gem to .gemfile
  2. Run rails generate devise:install
  3. Follow current device_token_auth's installation instructions

Luiyit avatar Sep 09 '22 16:09 Luiyit