0

从全新create-repack-app安装。我将以下内容添加到我的Gemfilethen 运行中bundle

gem 'devise_token_auth'

然后我运行:

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

创建数据库(开发和测试)并生成 ruby​​ 文件(包括对config/routes.rb文件的添加)。现在尝试任何rakerails命令执行以下操作:

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

注释掉config/routes.rb文件中的以下内容:

mount_devise_token_auth_for 'User', at: 'auth'

删除此错误。添加到User模型的代码不会导致此错误。我rails g devise:install也需要跑步吗?该文档没有提及任何额外的内容。所以我不确定我做错了什么。

4

2 回答 2

1

将以下代码添加到用户模型

extend Devise::Models

我的用户模型看起来像这样。

# 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
于 2019-09-21T14:19:11.990 回答
0

我从另一篇文章中找到了答案:Devise_token_auth 冲突?

添加以下设计初始化程序:

配置/设计.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

修复了问题。

于 2019-09-07T14:22:27.133 回答