2

I'm getting the following error when trying to use the :confirmable module of Devise:

NameError (undefined local variable or method `confirmed_at' for #<AdminUser:0x007f27841d0f30>)

My model:

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

I want to send a confirmation email when AdminUser is created.

Drenmi
  • 8,492
  • 4
  • 42
  • 51

3 Answers3

7

It is not enough to merely add the :confirmable option in the model, you'll also need to add the columns required by Devise into your database table.

Assuming you are using a model 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
Drenmi
  • 8,492
  • 4
  • 42
  • 51
3

Simply run the command:

rails g migration add_confirmable_to_devise

db/migrate/YYYYMMDDxxx_add_confirmable_to_devise.rb

class AddConfirmableToDevise < ActiveRecord::Migration
  # Note: You can't use change, as User.update_all will fail in the down migration
  def up
    add_column :users, :confirmation_token, :string
    add_column :users, :confirmed_at, :datetime
    add_column :users, :confirmation_sent_at, :datetime
    add_index :users, :confirmation_token, unique: true
    execute("UPDATE users SET confirmed_at = NOW()")
  end

  def down
    remove_columns :users, :confirmation_token, :confirmed_at, :confirmation_sent_at
  end
end

For further reference devise

Prashant4224
  • 1,551
  • 1
  • 14
  • 21
2

You put :confirmable in your AdminUser model. So you need to have columns for confirmable functions including confirmed_at.

You should have a migration file which Devise generated. In that file, you need to remove comment out for confirmable columns including created_at.

shirakia
  • 2,369
  • 1
  • 22
  • 33