Betjamin Richards
Betjamin Richards

Reputation: 941

Devise: Allow users to edit their account without providing a password BUT also use 'reconfirmable' functionality

I've been following this how-to in the Devise wiki...

How To: Allow users to edit their account without providing a password

...to enable my users to change the account credential and update without providing their existing password.

However, I also want to use the Confirmable modules reconfirmable functionality

Even though I have config.reconfirmable = true set in my devise initializer file the controller doesn't seem to be sending the reconfirmable emails.

Any ideas what's wrong?

Upvotes: 1

Views: 542

Answers (1)

Betjamin Richards
Betjamin Richards

Reputation: 941

The issue was that the controller in the original How-to had various missing Devise methods required by the Confirmable module.

To get it to work I had to use as much of the original update action as possible from the Devise::RegistrationsController and also override the update_resource(resource, params) action so that it didn't need a password to update the record.

So my bespoke RegistrationsController now looks like this:

class Users::RegistrationsController < Devise::RegistrationsController

  def update
    update_params = account_update_params
    # required for settings form to submit when password is left blank
    if update_params[:password].blank?
      [:password,:password_confirmation,:current_password].collect{|p| update_params.delete(p) }
    end
    if update_params[:password].present? and update_params[:current_password].blank?
      [:current_password].collect{|p| update_params.delete(p) }
    end

    self.resource = resource_class.to_adapter.get!(send(:"current_#{resource_name}").to_key)
    prev_unconfirmed_email = resource.unconfirmed_email if resource.respond_to?(:unconfirmed_email)

    if update_resource(resource, update_params)
      yield resource if block_given?
      if is_flashing_format?
        flash_key = update_needs_confirmation?(resource, prev_unconfirmed_email) ?
          :update_needs_confirmation : :updated
        set_flash_message :notice, flash_key
      end
      sign_in resource_name, resource, bypass: true
      respond_with resource, location: after_update_path_for(resource)
    else
      clean_up_passwords resource
      respond_with resource
    end
  end

  protected

  def update_resource(resource, params)
    resource.update_attributes(params)
  end
end

Hope this helps somebody!

Upvotes: 3

Related Questions