Devise :: SessionsController#中的NoMethodError创建未定义的方法“ current_sign_in_at” [英] NoMethodError in Devise::SessionsController#create undefined method `current_sign_in_at'
问题描述
当我尝试在Rails应用程序中登录时出现以下错误。我使用devise进行身份验证。我的错误是Devise :: SessionsController#create
未定义方法`current_sign_in_at'中的
NoMethodError
I am getting the following error when I try to sign in in my rails app. I used devise for authentication. My error is NoMethodError in Devise::SessionsController#create undefined method `current_sign_in_at'
我的用户模型是
models/user.rb
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable
attr_accessible :admin,:first_name, :last_name, :profile_name, :college_name, :email, :password, :password_confirmation, :remember_me, :provider, :uid
def admin?
end
def self.find_for_facebook_oauth(auth)
where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
user.provider = auth.provider
user.uid = auth.uid
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
#user.name = auth.info.name # assuming the user model has a name
#user.image = auth.info.image # assuming the user model has an image
user.save!
end
end
end
我的模式是
db/schema.rb
ActiveRecord::Schema.define(:version => 20140126101946) do
create_table "levels", :force => true do |t|
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "users", :force => true do |t|
t.string "first_name"
t.string "last_name"
t.string "profile_name"
t.string "college_name"
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.string "provider"
t.string "uid"
t.boolean "admin", :default => false
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
end
devise_create_users.rb中的代码是
The code in my devise_create_users.rb is
class DeviseCreateUsers < ActiveRecord::Migration
def change
create_table(:users) do |t|
t.string :first_name
t.string :last_name
t.string :profile_name
t.string :college_name
## 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
t.datetime :current_sign_in_at
t.datetime :last_sign_in_at
t.string :current_sign_in_ip
t.string :last_sign_in_ip
我的可追踪对象已被注释,我删除了注释,然后运行rake db:migrate,但是什么也没发生。现在,我无法根据需要删除可跟踪的内容。我需要的是以某种方式可以保留我的设计,并以使其也添加到schema.rb的方式向其添加可跟踪的信息。
My trackable was commented and I removed the comments and then ran rake db:migrate but nothing happened. Now I cannot remove trackable as I need it. What I need is that somehow I can keep my devise as it is and also add trackable to it in such a way that it gets added to the schema.rb as well.
推荐答案
要将新列添加到现有表中,仅更新已运行的迁移将不起作用,因为该模式的版本比现有迁移的版本高。如果您希望修改现有的迁移,则可以使用以下命令运行 down
迁移:
To add new columns to existing tables, simply updating already run migration is not going to work as the schema is at a later version than your existing migration's version. If you wish to modify the existing migration, you could run a down
migration with:
rake db:migrate:down VERSION=20140126101944 # use version of the user migration
然后修改迁移操作,并添加新列,然后使用以下方法运行 up
迁移:
Then modify the migration adding the new columns as you've already done, then run up
migration using:
rake db:migrate:up VERSION=20140126101944 # use version of the user migration
一种更好的方法是,如果您的应用程序已经投入生产,则使用更改添加一个新的迁移。
A better approach is to add a new migration with the change if your application is already in production.
添加可跟踪
到现有 users
表的列:
class AddTrackableColumnsToUser < ActiveRecord::Migration
def change
change_table :users do |t|
## Trackable
t.add_column :sign_in_count, :integer, :default => 0
t.add_column :current_sign_in_at, :datetime
t.add_column :last_sign_in_at, :datetime
t.add_column :current_sign_in_ip, :string
t.add_column :last_sign_in_ip, :string
end
end
end
然后运行 db:migrate
:
rake db:migrate
这篇关于Devise :: SessionsController#中的NoMethodError创建未定义的方法“ current_sign_in_at”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!