NoMethodError:使用Devise_token_auth的Rails gem无法为#User:0x000055ce01dcf0a8提供未定义的方法“ current_sign_in_at”

Wil*_*ese 1 ruby ruby-on-rails devise-token-auth

NoMethodError:#User:0x000055ce01dcf0a8的未定义方法`current_sign_in_at'

我认为这是某种会话方法错误

我有一个用于前端的angular6应用程序和用于后端的rails,所以对我来说最好的选择是选择devise_token_auth和ng_token_auth进行用户身份验证。

我安装了devise_token_auth gem,然后在终端中执行此行代码

"rails generate devise_token_auth:install User auth"

and on migration there was an error, I solved the issue by adding

"extend Devise::Models"

to the USER model and then migration had worked, then I created a user in the backend and tried to call sign_in using postman and the error "NoMethodError: undefined method `current_sign_in_at' for #User:0x000055ce01dcf0a8" came

I want the user to get authenticated using this gem or some other gem if they exist

oli*_*vrg 5

我最近遇到了这个问题,事实证明我的迁移中没有可跟踪的字段。有两种方法可以解决此问题:

选项一。添加一个新迁移,将可跟踪字段添加到用户

## Trackable
t.integer  :sign_in_count, default: 0, null: false
t.datetime :current_sign_in_at
t.datetime :last_sign_in_at
t.inet     :current_sign_in_ip
t.inet     :last_sign_in_ip
Run Code Online (Sandbox Code Playgroud)

运行rake db:migrate

第二种选择:运行向下迁移

从此命令开始-添加您的迁移版本号

rake db:migrate:down VERSION=xxxxxxxxxxxxxx
Run Code Online (Sandbox Code Playgroud)

然后,您应该能够将可跟踪字段添加到迁移文件中,然后运行

rake db:migrate up VERSION=xxxxxxxxxxxxxx
Run Code Online (Sandbox Code Playgroud)

运行rake db:migrate