I try to do that. Unfortunately I have problems with overriding update and I don't know how to do that correctly. The way I do that in another place is:
if params[:user][:password].blank?
params[:user].delete("password")
params[:user].delete("password_confirmation")
end
# ...
user.save!
So I tried to override update
def update
if params[:user][:password].blank?
params[:user].delete("password")
params[:user].delete("password_confirmation")
end
super
end
But it doesn't works. I still get can't be blank
near to the password input. How to achieve expected behaviour?
I take the answer of @anonymousxxx and add the following:
If you're using rails admin, you can override the controller "User" as follows:
#app/admin/user.rb
controller do
def update
if params[:user][:password].blank? && params[:user][:password_confirmation].blank?
params[:user].delete("password")
params[:user].delete("password_confirmation")
end
super
end
end
You should also note that, if you have the User model validations, specify when such validations are applied, for example:
validates :name, :email, presence: true
validates :password, :password_confirmation, presence: true, on: :create
validates :password, confirmation: true
This allows me to validate password presence only when I create a new user and update without changing his password.
I hope this is helpful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With