Created
January 23, 2015 20:56
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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