25

I would like to use the devise option :reconfirmable in my user model, so whenever a user changes his email, he needs to confirm it with a link sent by email.

The big problem is, that the email gets never sent ...

My setup is with devise 2.1.2 is:

user model:

attr_accessible: unconfirmed_email, ...

devise :invitable, :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable, :omniauthable

in the initilizer devise.rb:

config.reconfirmable = true

in the routes:

devise_for :users

in the form the model field unconfirmed_email gets set properly. I checked this through the console.

The first confirmation email, when a user registers on the page gets send out without problem.

I tried debugging the problem with adding this code to the initializers directory to overwrite the devise methode that gets triggered as a after_update hook:

module Devise::Models::Confirmable
  def send_confirmation_instructions
    debugger
  end
end

it seems like send_confirmation_instructions is never called, since I never get to the debugger.

Do I somehow need to call reconfirmable, or does it gets triggered automatically when setting the model attribute "unconfirmed_email" to a new email address?

Thankfull for any help, j.

user1311103
  • 711
  • 1
  • 6
  • 9
  • 1
    For reconfirmable to work you have to add a new column to your User table 't.string :unconfirmed_email # Only if using reconfirmable' Have you already done this ? – Himshwet Jul 09 '12 at 06:48
  • hi janders. thats all set up, I have the attribute in my user model and in attr_accessible ... I still havent solved this problem, so any help is very wellcome. – user1311103 Jul 09 '12 at 10:49
  • I am sorry, can't figure out what's wrong – Himshwet Jul 10 '12 at 07:29

1 Answers1

46

OK, this is embarrassing..

After diving into the Devise code, I figured out that you don't need to set the unconfirmed_email attribute of your user model, but just change the existing email attribute. The attribute unconfirmed_email is just used internally for Devise to store the email address until it's confirmed.

Later version of devise gem explains this in initial migration. Here is "Confirmable" section (note the comment on the last line) from XXX_devise_create_users.rb migration:

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

Sorry for bothering, but hopefully this can help somebody having the same problem...

Roman Pushkin
  • 5,639
  • 3
  • 40
  • 58
user1311103
  • 711
  • 1
  • 6
  • 9
  • 4
    Thanks for this. Good to know that Devise "automagically" populates `unconfirmed_email` when I update `email` in the model. Don't forget to mark your answer as "Accepted" (yes, even though you answered it yourself!). – Mark Berry Sep 12 '12 at 23:19
  • 1
    Yes... You thought right.. even if it was after 2 years.. it was helpfull to me. :) – Alfie Jan 09 '15 at 05:06