gpt4 book ai didi

ruby-on-rails - 设计中的 NoMethodError::SessionsController#create 未定义方法 `current_sign_in_at'

转载 作者:行者123 更新时间:2023-12-04 18:08:20 25 4
gpt4 key购买 nike

当我尝试登录 Rails 应用程序时出现以下错误。我使用设计进行身份验证。我的错误是
设计中的 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 中。

最佳答案

要将新列添加到现有表中,仅更新已运行的迁移是行不通的,因为架构的版本高于现有迁移的版本。如果你想修改现有的迁移,你可以运行 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 table :
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

关于ruby-on-rails - 设计中的 NoMethodError::SessionsController#create 未定义方法 `current_sign_in_at',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21383860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com