0

From a clean create-repack-app install. I add the following to my Gemfile then run bundle:

gem 'devise_token_auth'

Then I run:

rake db:create
rails g devise_token_auth:install
rake db:migrate

Databases (dev and test) are created and ruby files generated (including an addition to the config/routes.rb file). Trying any rake or rails command does the following right now:

rake routes
rake aborted!
NoMethodError: undefined method `devise' for User (call 'User.connection' to establish a connection):Class

Commenting out the following in the config/routes.rb file:

mount_devise_token_auth_for 'User', at: 'auth'

Removes this error. The code added to the User model doesn't contribute to this error. Do I need to run rails g devise:install also? The documentation doesn't mention anything extra. So I'm not sure what I'm doing wrong.

aarona
  • 35,986
  • 41
  • 138
  • 186

2 Answers2

1

Add below code to the User model

extend Devise::Models

My User model looks like this.

# frozen_string_literal: true

class User < ActiveRecord::Base
  extend Devise::Models
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable, :trackable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
  include DeviseTokenAuth::Concerns::User
end
0

I found my answer from another post: Devise_token_auth conflicts?

Adding the following Devise initializer:

config/devise.rb:

Devise.setup do |config|
  # The e-mail address that mail will appear to be sent from
  # If absent, mail is sent from "please-change-me-at-config-initializers-devise@example.com"
  config.mailer_sender = "support@myapp.com"

  # ==> 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/active_record'

  # If using rails-api, you may want to tell devise to not use ActionDispatch::Flash
  # middleware b/c rails-api does not include it.
  # See: https://stackoverflow.com/q/19600905/806956
  config.navigational_formats = [:json]
end

Fixed the problem.

aarona
  • 35,986
  • 41
  • 138
  • 186