使用Devise时未定义的方法confirmed_at

2 ruby ruby-on-rails devise devise-confirmable

尝试使用:confirmableDevise模块时出现以下错误:

NameError (undefined local variable or method `confirmed_at' for #<AdminUser:0x007f27841d0f30>)
Run Code Online (Sandbox Code Playgroud)

我的模特:

class AdminUser < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable,:recoverable, :rememberable, :trackable, :validatable, :confirmable

  after_create { |admin| admin.send_reset_password_instructions }
  def password_required?
    new_record? ? false : super
  end
end
Run Code Online (Sandbox Code Playgroud)

我想在AdminUser创建时发送确认电子邮件.

Dre*_*nmi 7

仅仅:confirmable在模型中添加选项是不够的,您还需要将Devise所需的列添加到数据库表中.

假设您使用的是模型AdminUser:

class AddConfirmableToDevise < ActiveRecord::Migration
  def self.up
    add_column :admin_users, :confirmation_token,   :string
    add_column :admin_users, :confirmed_at,         :datetime
    add_column :admin_users, :confirmation_sent_at, :datetime
    add_column :admin_users, :unconfirmed_email,    :string

    add_index  :admin_users, :confirmation_token, :unique => true
  end

  def self.down
    remove_index  :admin_users, :confirmation_token

    remove_column :admin_users, :unconfirmed_email
    remove_column :admin_users, :confirmation_sent_at
    remove_column :admin_users, :confirmed_at
    remove_column :admin_users, :confirmation_token
  end
end
Run Code Online (Sandbox Code Playgroud)