1

在我的 rails 5 应用程序中,我使用“devise_token_auth”gem 来开发基于令牌的 API 以进行身份​​验证。到目前为止,一切都运行良好,但突然间我遇到了错误,

NoMethodError (undefined method `create_token' for #<User:0x00000002268570>

在谷歌上找不到任何解决方案。以下是完整的错误,

Started POST "/api/v1/auth/sign_in" for ::1 at 2018-06-05 16:32:34 +0530
   (28.8ms)  SET NAMES utf8,  @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'),  @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
   (11.0ms)  SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC
Processing by DeviseTokenAuth::SessionsController#create as */*
  Parameters: {"email"=>"admin@example.com", "password"=>"[FILTERED]", "session"=>{"email"=>"admin@example.com", "password"=>"[FILTERED]"}}
Unpermitted parameter: :session
Unpermitted parameter: :session
  User Load (12.3ms)  SELECT  `users`.* FROM `users` WHERE (BINARY email = 'admin@example.com' AND provider='email') ORDER BY `users`.`id` ASC LIMIT 1
Unpermitted parameter: :session
Unpermitted parameter: :session
Completed 500 Internal Server Error in 395ms (ActiveRecord: 92.9ms)

NoMethodError (undefined method `create_token' for #<User:0x00000002268570>
Did you mean?  created_at):
4

1 回答 1

0

我通过使用文档的排除模块部分解决了这个错误。

# app/models/user.rb
class User < ActiveRecord::Base

  # notice this comes BEFORE the include statement below
  # also notice that :confirmable is not included in this block
  devise :database_authenticatable, :recoverable,
         :trackable, :validatable, :registerable,
         :omniauthable

  # note that this include statement comes AFTER the devise block above
  include DeviseTokenAuth::Concerns::User
end

包括 DeviseTokenAuth::Concerns::User 是解决未定义方法问题的原因,但我需要这一部分,因为它需要确认(我将包含放在设计块之前)

于 2019-01-19T03:13:12.987 回答