Home>

Create an application according to "Introduction to Ruby on Rails 5",
I am trying to display the login time after login authentication.
I installed Devise, but Trackable wasn't built in, so
Uncomment t.datetime: current_sign_in_at,
I wrote it to read in devise, but I got the following error.

Error message

NoMethodError in Devise :: SessionsController # create
undefined method `current_sign_in_at'for #<Account: 0x0000000c4ad1f0>

else
        match = matched_attribute_method (method.to_s)
        match? attribute_missing (match, * args,&amp;block): super<-error part
      end
    end
Applicable source code

helo_controller.rb

class HeloController&lt;ApplicationController
    layout 'application'
    before_action: authenticate_account !, only:: login_check
    def index
        @msg = 'this is sample page.'
    end
    def login_check
        @account = current_account
        @msg = 'you logined at:' + @ account.current_sign_in_at.to_s
    end
end

20180826062337_devise_create_accounts

# frozen_string_literal: true
class DeviseCreateAccounts&lt;ActiveRecord :: Migration [5.0]
  def change
    create_table: accounts do | t |
      ## Database authenticatable
      t.string: email, null: false, default: ""
      t.string: encrypted_password, null: false, default: ""
      ## Recoverable
      t.string: reset_password_token
      t.datetime: reset_password_sent_at
      ## Rememberable
      t.datetime: remember_created_at
      ## Trackable
      # t.integer: sign_in_count, default: 0, null: false
      t.datetime: current_sign_in_at
      # t.datetime: last_sign_in_at
      # t.string: current_sign_in_ip
      # t.string: last_sign_in_ip
      ## Confirmable
      # t.string: confirmation_token
      # t.datetime: confirmed_at
      # t.datetime: confirmation_sent_at# t.string: unconfirmed_email # Only if using reconfirmable
      ## Lockable
      # t.integer: failed_attempts, default: 0, null: false # Only if lock strategy is: failed_attempts
      # t.string: unlock_token # Only if unlock strategy is: email or: both
      # t.datetime: locked_at

      t.timestamps null: false
    end
    add_index: accounts,: email, unique: true
    add_index: accounts,: reset_password_token, unique: true
    # add_index: accounts,: confirmation_token, unique: true
    # add_index: accounts,: unlock_token, unique: true
  end
end

account.rb

class Account&lt;ApplicationRecord
  '# Include default devise modules. Others available are:
  '#: confirmable,: lockable,: timeoutable,: trackable and: omniauthable
  devise: database_authenticatable,: registerable,
         : recoverable,: rememberable,: trackable,: validatable
end

schema.rb

ActiveRecord :: Schema.define (version: 20180826062337) do
  create_table "accounts", force:: cascade do | t |
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["email"], name: "index_accounts_on_email", unique: true
    t.index ["reset_password_token"], name: "index_accounts_on_reset_password_token", unique: true
  end
Tried

Delete "[email protected]_sign_in_at.to_s" from helo_controller.rb,
If i reset Devise to its initial state, the app will work properly.
(You can log in, but the login time is not displayed.)

Add t.datetime: current_sign_in_at to schema.rb,
When migrating (rails db: migrate),
t.datetime: current_sign_in_at disappears from schema.rb.

The following post was the same phenomenon, so I tried to specify version with rails db: migrate down and up,
It was displayed that there was no corresponding version.
https://stackoverflow.com/questions/21383860/nomethoderror-in-devisesessionscontrollercreate-undefined-method-current-sig

Supplemental information (FW/tool version etc.)

Windows 8.1 64-bit
ruby 2.2.6p396
Rails 5.0.7
devise 4.5.0

  • Answer # 1

    From devise 4.5.0: trackable seems to have been removed, so if you reverted to the previous devise version, the login time will be displayed.
    Leave the procedure of the trial and error results such as deleting the account model.

    ・ Check the devise version from the following page
    Https://github.com/plataformatec/devise/blob/master/CHANGELOG.md
    ・ Manually delete the following files
    ・ Gemfile.lock
    
    Https://code-schools.com/ror-bundleerror/
    ・ Enter "gem 'devise', '~>4.4.3'" in Gemfile
    
    Http://taniguhi.hatenablog.com/entry/2013/07/31/224551
    ・ Command prompt bundle install
    -Check devise (4.4.3) in Gemfile.lock
    ・ Command prompt (app directory)>rails generate devise: install
    ・ Rails destroy devise account
    
    Https://note.mu/oreno/n/n45f8208ade29
    ・ Manually delete the following files
    Db/development.sqlite3
    Db/migrate/20180826062337_devise_create_accounts.rb
    
    Https://StackOverflow.com/questions/67393
    ・ Rails db: migrate
    ・ Rails generate devise account
    ・ Rails db: migrate
    ・ Manually delete the following files
    Db/development.sqlite3
    ・ Rails db: migrate