Devise中的NoMethodError :: SessionsController #create undefined method`current_sign_in_at'

时间:2014-01-27 14:49:28

标签: ruby-on-rails devise nomethoderror

当我尝试在rails应用中登录时出现以下错误。我使用devise进行身份验证。我的错误是     Devise中的NoMethodError :: SessionsController #create     未定义的方法`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中的代码是

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中。

2 个答案:

答案 0 :(得分:5)

要向现有表添加新列,只是更新已运行的迁移不会起作用,因为架构的版本比现有迁移的版本更高。如果您希望修改现有迁移,可以使用以下命令运行down迁移:

rake db:migrate:down VERSION=20140126101944 # use version of the user migration

然后按照您已经完成的方式修改添加新列的迁移,然后使用以下命令运行up迁移:

rake db:migrate up VERSION=20140126101944 # use version of the user migration

如果您的应用程序已在生产中,更好的方法是使用更改添加新迁移。

trackable列添加到现有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

答案 1 :(得分:3)

在您的models/user.rb

更改

devise :database_authenticatable, :registerable, :recoverable, :rememberable, 
       :trackable, :validatable, :omniauthable

devise :database_authenticatable, :registerable, :recoverable, :rememberable,
       :validatable, :omniauthable

即删除 :trackable