Last active
August 29, 2015 14:13
-
-
Save jameshibbard/a4dff800964129fc1cd1 to your computer and use it in GitHub Desktop.
Devise / cancancan tutorial: UsersController#update
This file contains 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
def update | |
if user_params[:password].blank? | |
user_params.delete(:password) | |
user_params.delete(:password_confirmation) | |
end | |
successfully_updated = if needs_password?(@user, user_params) | |
@user.update(user_params) | |
else | |
@user.update_without_password(user_params) | |
end | |
respond_to do |format| | |
if successfully_updated | |
format.html { redirect_to @user, notice: 'User was successfully updated.' } | |
format.json { head :no_content } | |
else | |
format.html { render action: 'edit' } | |
format.json { render json: @user.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
protected | |
def needs_password?(user, params) | |
params[:password].present? | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment