Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveAdmin: how to leave user password unchanged?

I am using ActiveAdmin as my administration backend in my rails app. Basically, I have an admin_user and a user model.

When I create a new user from an admin account, I specify an email and a password, that is ok.

Let's say I then want to modify the user's email but not the password... it seems this cannot be done as the password field cannot be blank when updating a user.

Is there a configuration somewhere that would consider that the password is unchanged is the fields (password and password_confirmation) are left blank while updating a user?

like image 655
Luc Avatar asked Mar 08 '13 10:03

Luc


2 Answers

You don't really need to mess at all with Devise's registration controller, you can just ignore empty password fields inside ActiveAdmin's resource controller:

ActiveAdmin.register User do   controller do      def update       model = :user        if params[model][:password].blank?         %w(password password_confirmation).each { |p| params[model].delete(p) }       end        super     end   end end 
like image 77
mauriciomdea Avatar answered Oct 09 '22 00:10

mauriciomdea


Devise provides an update_without_password method that you can use when updating a user if no password is entered. Using that method, you can customize the update method in your ActiveAdmin users controller.

 def update    @user = User.find(params[:id])    if params[:user][:password].blank?      @user.update_without_password(params[:user])    else      @user.update_attributes(params[:user])    end    if @user.errors.blank?      redirect_to admin_users_path, :notice => "User updated successfully."    else      render :edit    end  end 

The Devise Wiki has more information about this method if your interested.

like image 45
rocket scientist Avatar answered Oct 09 '22 00:10

rocket scientist