Ruby-on-rails #User 的未定义局部变量或方法“confirmed_at”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9108460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
undefined local variable or method `confirmed_at' for #User
提问by riship89
I am using Rails 3. There is a possible duplicate here. But it did not solve my problem, neither did any other solution.
我使用Rails 3有可能重复的记录在这里。但它没有解决我的问题,也没有解决任何其他解决方案。
My migration is as follows
我的迁移如下
class AddConfirmableToDevise < ActiveRecord::Migration
def change
change_table(:users) do |t|
t.confirmable
end
add_index :users, :confirmation_token, :unique => true
end
end
I do have devise :confirmableadded in Usermodel.
我确实devise :confirmable添加了User模型。
My rake db:migrategives no output. and my sign up page gives the error:
我的rake db:migrate没有输出。我的注册页面给出了错误:
undefined local variable or method 'confirmed_at' for #User
Anybody has a clue?
有人有线索吗?
回答by riship89
Ok. I solved it. The migration is outdated. Generate new migration with same code but another name.
好的。我解决了。迁移已过时。使用相同的代码但不同的名称生成新的迁移。
1.Run command:
1.运行命令:
rails g migration add_confirmable_to_devise_v1
2.In the migration file:
2.在迁移文件中:
class AddConfirmableToDeviseV1 < ActiveRecord::Migration
def change
change_table(:users) do |t|
t.confirmable
end
add_index :users, :confirmation_token, :unique => true
end
end
3.Then
3.然后
rake db:migrate
回答by SomeDudeSomewhere
As of the latest devise, you just need to remove comments from the following lines on the devise users migration.. (2013....._devise_create_users.rb)
对于最新的设计,您只需要从以下几行中删除有关设计用户迁移的评论.. (2013....._devise_create_users.rb)
# Confirmable
t.string :confirmation_token
t.datetime :confirmed_at
t.datetime :confirmation_sent_at
t.string :unconfirmed_email # Only if using reconfirmable
回答by sameers
To tie in @DevDude's answer with the accepted answer - if you already have an existing Usersmodel to which you need to add confirmable, the full migration code for the version of Devise current as of 4/14 is:
要将@DevDude 的答案与已接受的答案联系起来 - 如果您已经有一个Users需要添加可确认的现有模型,则截至 4 月 14 日的 Devise 当前版本的完整迁移代码是:
class AddConfirmableToDeviseV1 < ActiveRecord::Migration
def change
change_table(:users) do |t|
# Confirmable
t.string :confirmation_token
t.datetime :confirmed_at
t.datetime :confirmation_sent_at
t.string :unconfirmed_email # Only if using reconfirmable
end
add_index :users, :confirmation_token, :unique => true
end
end
回答by AgainstPIT
Note for myself. Someone might find it helpful: What we need is 2 commands below:
本人注意。有人可能会发现它有帮助:我们需要的是以下 2 个命令:
rake db:migrate:reset
rake db:reset
Voila! It works!
瞧!有用!
回答by charliemagee
I'm using Mongoid and got this same error. I added these fields and got rspec to go green on my 16 examples.
我正在使用 Mongoid 并遇到同样的错误。我添加了这些字段并使 rspec 在我的 16 个示例中变为绿色。
field :confirmation_token, :type => String
field :confirmed_at, :type => Time
field :confirmation_sent_at, :type => Time
field :unconfirmed_email, :type => String

