ActiveAdmin: how to leave user password unchanged?

后端 未结 5 1562
天命终不由人
天命终不由人 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:32

    You can validate the password only on create because bcrypt when updating will still validate password presence.

    class User
       validate :password, :password_confirmation, presence: true, on: :create 
    end
    

    In my opinion this is much simpler while causing no risk, and allows you to use a single partial form for create and update routes with an if statement showing/not showing password input field like so:

    <%= form_for(user, url: users_path) do |form| %>
    <%= form.label 'Name' %>
    <%= form.text_field :name%>
    <%= form.label 'email' %>
    <%= form.text_field :email%>
    <%= form.label 'Password' %>
    <%= form.password_field :password%>
    **<% if form.object.new_record? %>** <%= form.label 'password_confirmation' %>
    <%= form.password_field :password_confirmation%>
    **<% end %>** <%= form.submit (form.object.new_record? ? 'create' : 'update') %>

提交回复
热议问题