在我的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):
答案 0 :(得分:0)
我通过使用文档的Excluding Modules部分解决了此错误。
# 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,但我需要此部分,因为它需要确认(我将include放在devise块之前)