Skip to content

Instantly share code, notes, and snippets.

@jameshibbard
Created January 23, 2015 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jameshibbard/8daa981a6bbc30485b4d to your computer and use it in GitHub Desktop.
Save jameshibbard/8daa981a6bbc30485b4d to your computer and use it in GitHub Desktop.
Overriding Devise's RegistrationsController allows us to add our own logic for handling updates
class RegistrationsController < Devise::RegistrationsController
def update
account_update_params = devise_parameter_sanitizer.sanitize(:account_update)
@user = User.find(current_user.id)
if needs_password?
successfully_updated = @user.update_with_password(account_update_params)
else
account_update_params.delete('password')
account_update_params.delete('password_confirmation')
account_update_params.delete('current_password')
successfully_updated = @user.update_attributes(account_update_params)
end
if successfully_updated
set_flash_message :notice, :updated
sign_in @user, :bypass => true
redirect_to edit_user_registration_path
else
render 'edit'
end
end
private
def needs_password?
@user.email != params[:user][:email] || params[:user][:password].present?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment