質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Q&A

解決済

1回答

5980閲覧

current_sign_in_atでログイン時刻を画面表示させたい

tenten52

総合スコア12

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

0グッド

0クリップ

投稿2018/08/27 12:12

編集2018/08/28 02:03

前提・実現したいこと

「Ruby on Rails 5 超入門」に沿ってアプリを作成し、
ログイン認証後にログイン時刻を表示させようとしています。
Deviseをインストールしましたが、Trackableが組み込まれていなかったので、
t.datetime :current_sign_in_atのコメントを外して、
deviseに読み込むように記述したのですが、以下のエラーが出ました。

発生している問題・エラーメッセージ

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

ruby

1  else 2 match = matched_attribute_method(method.to_s) 3 match ? attribute_missing(match, *args, &block) : super ←エラー部分 4 end 5 end

該当のソースコード

helo_controller.rb

ruby

1class HeloController < ApplicationController 2 layout 'application' 3 before_action :authenticate_account!, only: :login_check 4 5 def index 6 @msg = 'this is sample page.' 7 end 8 9 def login_check 10 @account = current_account 11 @msg = 'you logined at: ' + @account.current_sign_in_at.to_s 12 end 13end

20180826062337_devise_create_accounts

ruby

1# frozen_string_literal: true 2 3class DeviseCreateAccounts < ActiveRecord::Migration[5.0] 4 def change 5 create_table :accounts do |t| 6 ## Database authenticatable 7 t.string :email, null: false, default: "" 8 t.string :encrypted_password, null: false, default: "" 9 10 ## Recoverable 11 t.string :reset_password_token 12 t.datetime :reset_password_sent_at 13 14 ## Rememberable 15 t.datetime :remember_created_at 16 17 ## Trackable 18 #t.integer :sign_in_count, default: 0, null: false 19 t.datetime :current_sign_in_at 20 #t.datetime :last_sign_in_at 21 #t.string :current_sign_in_ip 22 #t.string :last_sign_in_ip 23 24 ## Confirmable 25 # t.string :confirmation_token 26 # t.datetime :confirmed_at 27 # t.datetime :confirmation_sent_at 28 # t.string :unconfirmed_email # Only if using reconfirmable 29 30 ## Lockable 31 # t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts 32 # t.string :unlock_token # Only if unlock strategy is :email or :both 33 # t.datetime :locked_at 34 35 36 t.timestamps null: false 37 end 38 39 add_index :accounts, :email, unique: true 40 add_index :accounts, :reset_password_token, unique: true 41 # add_index :accounts, :confirmation_token, unique: true 42 # add_index :accounts, :unlock_token, unique: true 43 end 44end

account.rb

ruby

1class Account < ApplicationRecord 2 '# Include default devise modules. Others available are: 3 '# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 4 devise :database_authenticatable, :registerable, 5 :recoverable, :rememberable, :trackable, :validatable 6end

schema.rb

ruby

1ActiveRecord::Schema.define(version: 20180826062337) do 2 3 create_table "accounts", force: :cascade do |t| 4 t.string "email", default: "", null: false 5 t.string "encrypted_password", default: "", null: false 6 t.string "reset_password_token" 7 t.datetime "reset_password_sent_at" 8 t.datetime "remember_created_at" 9 t.datetime "created_at", null: false 10 t.datetime "updated_at", null: false 11 t.index ["email"], name: "index_accounts_on_email", unique: true 12 t.index ["reset_password_token"], name: "index_accounts_on_reset_password_token", unique: true 13 end

試したこと

helo_controller.rbから「 + @account.current_sign_in_at.to_s」を消し、
Deviseを初期状態に戻すとアプリは正常に動作します。
(ログインできますが、ログイン時刻は表示されません。)

schema.rbにt.datetime :current_sign_in_atを追記して、
マイグレーション(rails db:migrate)をすると、
schema.rbからt.datetime :current_sign_in_atが消えてしまいます。

下記の投稿が同事象だったのでrails db:migrate downとupでversion指定をしてみましたが、
該当のversionがないと表示されました。
https://stackoverflow.com/questions/21383860/nomethoderror-in-devisesessionscontrollercreate-undefined-method-current-sig

補足情報(FW/ツールのバージョンなど)

Windows8.1 64ビット
ruby 2.2.6p396
Rails 5.0.7
devise 4.5.0

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

devise 4.5.0から:trackableが削除されたようなので、deviseのバージョンを一つ前に戻したら、ログイン時刻が表示されるようになりました。
accountのモデルの削除など試行錯誤しました結果の手順を残しておきます。

・下記のページからdeviseのバージョンを確認  https://github.com/plataformatec/devise/blob/master/CHANGELOG.md ・下記のファイルを手動で削除  ・Gemfile.lock  <参考>   https://code-schools.com/ror-bundleerror/ ・Gemfileに「gem 'devise', '~> 4.4.3'」を記入  <参考>   http://taniguhi.hatenablog.com/entry/2013/07/31/224551 ・コマンドプロンプト bundle install ・Gemfile.lockで、devise(4.4.3)を確認 ・コマンドプロンプト(該当のアプリのディレクトリ)>rails generate devise:install ・rails destroy devise account  <参考>   https://note.mu/oreno/n/n45f8208ade29 ・下記のファイルを手動で削除  db/development.sqlite3  db/migrate/20180826062337_devise_create_accounts.rb  <参考>   https://teratail.com/questions/67393 ・rails db:migrate ・rails generate devise account ・rails db:migrate ・下記のファイルを手動で削除  db/development.sqlite3 ・rails db:migrate

投稿2018/08/28 10:42

tenten52

総合スコア12

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問