Devise update user without password
Solution 1:
I think this is a much better solution:
if params[:user][:password].blank? && params[:user][:password_confirmation].blank?
params[:user].delete(:password)
params[:user].delete(:password_confirmation)
end
This prevents you from having to change the Devise controller by simply removing the password field from the form response if it is blank.
Just be sure to use this before @user.attributes = params[:user]
or whatever you use in your update
action to set the new parameters from the form.
Solution 2:
Is this what you're look for? From the Devise wiki
Allow users to edit their account without providing a password
It shows how to do it for rails 3 and rails 4
=======================
John's solution is a simpler alternative
Solution 3:
I think with Devise 3.2+ you can just override update_resource in your subclassed controller. Here's an example for the question originally asked:
class MyRegistrationsController < Devise::RegistrationsController
protected
def update_resource(resource, params)
resource.update_without_password(params)
end
end
Solution 4:
I solved this problem as follows: if form submitting with password, then need fill current_password field or form updated without password and current_password
in model:
class User
devise: bla-bla-bla
attr_accessor :current_password
end
In controller:
class Users::RegistrationsController < Devise::RegistrationsController
layout 'application'
def update
self.resource = resource_class.to_adapter.get!(send(:"current_#{resource_name}").to_key)
# custom logic
if params[:user][:password].present?
result = resource.update_with_password(params[resource_name])
else
result = resource.update_without_password(params[resource_name])
end
# standart devise behaviour
if result
if is_navigational_format?
if resource.respond_to?(:pending_reconfirmation?) && resource.pending_reconfirmation?
flash_key = :update_needs_confirmation
end
set_flash_message :notice, flash_key || :updated
end
sign_in resource_name, resource, :bypass => true
respond_with resource, :location => after_update_path_for(resource)
else
clean_up_passwords resource
respond_with resource
end
end
end
Solution 5:
If you still want to support password change but make it optional, check the availability of current_password
as such:
class MyRegistrationsController < Devise::RegistrationsController
protected
def update_resource(resource, params)
if params[:current_password].blank?
resource.update_without_password(params.except(:current_password))
else
resource.update_with_password(params)
end
end
end
That way if the current_password is present you can proceed and update the password, else ignore it and update without password.