hrishikeshp19
hrishikeshp19

Reputation: 9028

undefined local variable or method `confirmed_at' for #User

I am using Rails 3. There is a possible duplicate here. But it did not solve my problem, neither did any other solution.

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 :confirmable added in User model.

My rake db:migrate gives no output. and my sign up page gives the error:

undefined local variable or method 'confirmed_at' for #User

Anybody has a clue?

Upvotes: 22

Views: 25978

Answers (7)

wieczorek1990
wieczorek1990

Reputation: 8098

I reordered some extend/include statements and had this error.

Upvotes: 0

Bello Tomi
Bello Tomi

Reputation: 973

After adding confirmable to your user model. The devise gem needs your application to have the confirmation_token, confirmed_at, confirmed_sent_at, unconfirmed_email and confirmation_token in the user table.

  1. Generate a new migration using the command below

    rails g migration add_confirmable_to_devise

  2. Add the code to the newly created migration file

class AddConfirmableToDevise < ActiveRecord::Migration[7.0]
def up
    add_column :users, :confirmation_token, :string
    add_column :users, :confirmed_at, :datetime
    add_column :users, :confirmation_sent_at, :datetime
    add_column :users, :unconfirmed_email, :string
    add_index :users, :confirmation_token, unique: true
    User.update_all confirmed_at: DateTime.now
end
def down
    remove_index :users, :confirmation_token
    remove_columns :users, :confirmation_token, :confirmed_at, :confirmation_sent_at
  end
end
  1. Run the migrations

    rails db:migrate

Upvotes: 2

sameers
sameers

Reputation: 5095

To tie in @DevDude's answer with the accepted answer - if you already have an existing Users model to which you need to add confirmable, the full migration code for the version of Devise current as of 4/14 is:

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

Upvotes: 17

SomeDudeSomewhere
SomeDudeSomewhere

Reputation: 3940

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)

  # Confirmable
  t.string   :confirmation_token
  t.datetime :confirmed_at
  t.datetime :confirmation_sent_at
  t.string   :unconfirmed_email # Only if using reconfirmable

Upvotes: 21

AgainstPIT
AgainstPIT

Reputation: 401

Note for myself. Someone might find it helpful: What we need is 2 commands below:

    rake db:migrate:reset 
    rake db:reset

Voila! It works!

Upvotes: 17

charliemagee
charliemagee

Reputation: 693

I'm using Mongoid and got this same error. I added these fields and got rspec to go green on my 16 examples.

field :confirmation_token,   :type => String
field :confirmed_at,         :type => Time
field :confirmation_sent_at, :type => Time
field :unconfirmed_email,    :type => String

Upvotes: 4

hrishikeshp19
hrishikeshp19

Reputation: 9028

Ok. I solved it. The migration is outdated. Generate new migration with same code but another name.

1.Run command:

rails g migration add_confirmable_to_devise_v1

2.In the migration file:

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

rake db:migrate

Upvotes: 29

Related Questions