未定义的局部变量或#User的方法`confirmed_at'

ris*_*p89 18 ruby-on-rails devise

我使用Rails 3有可能重复的记录在这里.但它没有解决我的问题,也没有解决任何其他问题.

我的迁移如下

class AddConfirmableToDevise < ActiveRecord::Migration
  def change
    change_table(:users) do |t| 
      t.confirmable 
    end
    add_index  :users, :confirmation_token, :unique => true 
  end
end
Run Code Online (Sandbox Code Playgroud)

我确实devise :confirmable添加了User模型.

rake db:migrate没有输出.我的注册页面给出了错误:

undefined local variable or method 'confirmed_at' for #User
Run Code Online (Sandbox Code Playgroud)

有人有线索吗?

ris*_*p89 26

好.我解决了 迁移已过时.使用相同的代码生成新的迁移,但使用其他名称.

1.运行命令:

rails g migration add_confirmable_to_devise_v1
Run Code Online (Sandbox Code Playgroud)

2.在迁移文件中:

class AddConfirmableToDeviseV1 < ActiveRecord::Migration
  def change
    change_table(:users) do |t| 
      t.confirmable 
    end
    add_index  :users, :confirmation_token, :unique => true 
  end
end
Run Code Online (Sandbox Code Playgroud)

3.Then

rake db:migrate
Run Code Online (Sandbox Code Playgroud)


Som*_*ere 19

从最新设计开始,您只需要在设计用户迁移中删除以下行中的注释.(20​​13 ....._ 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
Run Code Online (Sandbox Code Playgroud)


sam*_*ers 16

将@ 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
Run Code Online (Sandbox Code Playgroud)


Aga*_*PIT 14

请注意自己.有人可能会发现它有用:我们需要的是以下2个命令:

    rake db:migrate:reset 
    rake db:reset
Run Code Online (Sandbox Code Playgroud)

瞧!有用!