LoginSignup
4
5

More than 3 years have passed since last update.

【Rails】NoMethodError (undefined method `current_sign_in_at' for #<User:XXXXXXXXXXXXXX>):

Posted at

はじめに

Railsでgem devise_token_authを導入時に発生した内容です。

Eメール認証の確認をしようと、
http://localhost:3000/api/v1/auth/sign_in
にPOSTリクエストを出すと以下エラーが発生しました。

Started POST "/api/v1/auth/sign_in" for ... Processing by ... DeviseTokenAuth::SessionsController#create as JSON
...

Completed 500 Internal Server Error in 311ms (ActiveRecord: 17.4ms | Allocations: 7912)

NoMethodError (undefined method `current_sign_in_ip' for #<User:...>):

※rails serverより抜粋。

このエラーに対して、自分がうまくいった解決法を残します。

環境

OS: macOS Catalina 10.15.1
Ruby: 2.6.5
Rails: 6.0.2.1

結論:user.rbを修正

Before

app/models/user.rb
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  include DeviseTokenAuth::Concerns::User
end

After

app/models/user.rb
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
  include DeviseTokenAuth::Concerns::User
end

:trackableを削除すると無事に通るようになりました。

おわりに

最後まで読んで頂きありがとうございました:bow_tone1:

どなたかの参考になれば幸いです:relaxed:

参考にさせて頂いたサイト(いつもありがとうございます)

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5