AnApprentice
AnApprentice

Reputation: 111080

How to skip the need to confirm an email address update with devise?

I'm using rails with devise confirmable.

Generally I want to have users confirm their email address, however sometimes I need to manually change an email on behalf of an existing user and I want to skip sending the confirmation email.

When creating a new user I can skip the confirmation email with:

user.skip_confirmation!

...but this does not appear to work for existing, already confirmed users - insofar as the email attribute is not updated, and devise still requires the user to confirm the new email and sends out a confirmation email:

@user = User.find_by_email('[email protected]')
@user.email = '[email protected]'
@user.skip_confirmation!
@user.save!

Upvotes: 33

Views: 18571

Answers (6)

RailsDFW
RailsDFW

Reputation: 1583

The method you want to use is skip_reconfirmation! (note the 're' in reconfirmation).

@user = User.find_by_email('[email protected]')
@user.email = '[email protected]'
@user.skip_reconfirmation!
@user.save!

Upvotes: 101

Hassan Haroon
Hassan Haroon

Reputation: 134

If you need to skip reconfirmation for a specific user, you may go for following:

user = User.find_by(email: '[email protected]')
user.email = '[email protected]'
user.skip_reconfirmation!
user.save!

If you want to remove reconfirmation functionality from your application and update email as soon as you update it and not send a notification, go to config/devise.rb and change the value of

config.reconfirmable = true

to

config.reconfirmable = false

Note that, if you have reconfirmable option true in devise.rb, any email update will be saved in unconfirmed_email column in database until you comfirm it from the email.

Upvotes: 2

Marcelo Austria
Marcelo Austria

Reputation: 951

When updating the email use skip_reconfirmation!

skip_reconfirmation!

@user = User.find_by_email('[email protected]')
@user.email = '[email protected]'
@user.skip_reconfirmation!
@user.save!

Upvotes: 0

Vladimir
Vladimir

Reputation: 133

You can use this kind of code in your model # models/users.rb. This will disable email reconfirmation on update:

def postpone_email_change?
  false 
end

or in your controller

def update
  user.email = params[:user][:email]
  user.skip_confirmation_notification!

  if user.save
    user.confirm
  end
end

Upvotes: 7

Benjamin Crouzier
Benjamin Crouzier

Reputation: 41965

If you want to skip just the email notification, do this:

@user.skip_confirmation_notification!
@user.save

Upvotes: 1

Rodrigo Flores
Rodrigo Flores

Reputation: 2461

Try setting Devise.reconfirmable or User.reconfirmable (or whatever your model is) to false. You can set it on config/initializers/devise.rb on this line:

# If true, requires any email changes to be confirmed (exctly the same way as
# initial account confirmation) to be applied. Requires additional unconfirmed_email
# db field (see migrations). Until confirmed new email is stored in
# unconfirmed email column, and copied to email column on successful confirmation.
config.reconfirmable = true

You can also use Active Record's update_column method, which saves a field without running callbacks or validations.

Upvotes: 16

Related Questions