问题描述
我正在使用ActiveAdmin作为Rails应用程序中的管理后端。基本上,我有一个 admin_user
和一个 user
模型。
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.
在更新用户时,是否有某个配置会认为密码未更改,而字段(password和password_confirmation)却留为空白?
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?
推荐答案
Devise提供了 update_without_password
方法,您可以在没有输入密码的情况下更新用户。使用该方法,您可以在ActiveAdmin用户控制器中自定义更新方法。
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.
这篇关于ActiveAdmin:如何保持用户密码不变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!