ActiveAdmin: how to leave user password unchanged?

后端 未结 5 1560
天命终不由人
天命终不由人 2021-02-01 16:27

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 us

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-01 16:39

    You need to evaluate password and password_confirmation in the if statement, to apply the validations on "password_confirmation", eg for my case:

    #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
    
    
    #app/model/user.rb
    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.

    This work for me, I hope this is helpful.

    I hope this is helpful.

提交回复
热议问题