Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveAdmin - Edit Devise user without changing password

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?

like image 973
ciembor Avatar asked May 17 '13 13:05

ciembor


Video Answer


1 Answers

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.

like image 79
calrrox Avatar answered Oct 14 '22 12:10

calrrox