Create a gist now

Instantly share code, notes, and snippets.

Rails controller derived from `InheritedResources::Base` (see https://github.com/josevalim/inherited_resources) that plays nice with Devise (see http://devise.plataformatec.com.br/) when updating a user record that has no changes to password. Discussed on my blog at http://archlever.blogspot.com/2013/06/tools-save-you-time-except-when-they.html
# Actions for Users. Not necessarily a finer distinction one way or another yet.
class UsersController < InheritedResources::Base
# FIXME respond_to :json # should spec JSON actions, too
respond_to :html
load_and_authorize_resource
def update
# Devise has the convention that, for updates where the password is NOT to
# be modified, the password and password-confirmation fields MUST NOT be
# submitted. inherited_resources, of course, just sees two text fields and
# wraps them up along with the rest of the form. Oops.
remove_password_parameters if password_fields_are_empty?
super
end
private
def password_fields_are_empty?
params['user']['password'].empty? and
params['user']['password_confirmation'].empty?
end
def remove_password_parameters
params['user'].delete 'password'
params['user'].delete 'password_confirmation'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment