Skip to content

Instantly share code, notes, and snippets.

@jameshibbard
Last active August 29, 2015 14:13
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/a4dff800964129fc1cd1 to your computer and use it in GitHub Desktop.
Save jameshibbard/a4dff800964129fc1cd1 to your computer and use it in GitHub Desktop.
Devise / cancancan tutorial: UsersController#update
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