我使用devise
为authentication
和我有每个用户的role
,我允许与admin
角色的用户创建新的用户,我想管理员用户edit the password
为用户剩下的,如果他们忘记了自己的密码。但是我无法在编辑中改变没有当前密码的密码。那么我怎样才能允许管理员用户通过编辑用户密码来改变密码,并像我们为其余的值一样保存。如何在设计中更改没有当前密码的密码?
回答
有一个方法内置设计称为update_without_password
。
下面是我用我的更新方法是什么:
# PUT /manage_users/1
# PUT /manage_users/1.json
def update
@user = User.find(params[:id])
able_to_edit_profile?
# required for settings form to submit when password is left blank
if params[:user][:password].blank?
params[:user].delete("password")
params[:user].delete("password_confirmation")
end
respond_to do |format|
if @user.update_attributes(params[:user])
@user.save
# sign the user in with their new password so it doesn't redirect to the login screen
sign_in @user, :bypass => true
format.html {
flash[:notice] = 'User was successfully updated.'
redirect_to session.delete(:return_to)
}
format.json { head :no_content }
else
format.html { render action: "edit", notice: 'Error updating user.' }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
private
# If the user is not an admin and trying to edit someone else's profile, redirect them
def able_to_edit_profile?
if !current_user.try(:admin?) && current_user.id != @user.id
flash[:alert] = "That area is for administrators only."
redirect_to :root
end
end
实际上,如果我们想更改其他值而不提供密码,但我们无法更新密码,则可以使用此update_without_password。 – logesh 2013-03-26 14:39:34
我用'if params [:user] [:password] .blank? params [:user] .delete(“password”) params [:user] .delete(“password_confirmation”) end'但带有@ user.update_attributes(params [:user]),它现在可用。 – logesh 2013-03-28 15:17:44
啊很高兴知道。实际上,我昨天刚刚遇到了一个问题,并且注意到这段代码并未改变用户密码。我会用你的解决方案来解决我的问题。很高兴我们可以一起解决我们的两个问题。 – Catfish 2013-03-28 15:25:40
由于update_without_password
仍然需要current_password
更新密码,你就必须有一个update
这样的:
def update
# required for settings form to submit when password is left blank
if params[:user][:password].blank?
params[:user].delete("password")
params[:user].delete("password_confirmation")
end
@user = User.find(current_user.id)
if @user.update_attributes(params[:user])
set_flash_message :notice, :updated
# Sign in the user bypassing validation in case his password changed
sign_in @user, :bypass => true
redirect_to after_update_path_for(@user)
else
render "edit"
end
end
这示例用于更新当前用户(包括用户的密码),但您可以对其进行修改以适应您的需求。
是的,这是正确的答案 – 2015-10-08 10:08:25
- 1. 更改没有当前密码字段的密码在php
- 2. Rails 4,如何在密码更改时询问当前密码?
- 3. MVC验证更改密码。当前密码与新密码
- 4. 如何设计允许我更新密码而不通过当前密码?
- 5. 在设计中更改用户密码
- 6. Rails没有密码设计
- 7. Rails3用户更改密码,如何更改当前会话密码?
- 8. 在检索当前密码时更改asp.net用户密码
- 9. Rails 3.2 Omniauth设计 - 添加密码 - 跳过当前密码
- 10. 在设计用户上更改密码
- 11. 停止设计当前密码要求
- 12. Ruby on Rails,设计宝石。密码为空时如何删除当前密码?
- 13. 更改设计管理员的密码没有:可注册
- 14. 当我更改新密码时如何验证旧密码
- 15. C++ - 我的密码程序没有更改密码
- 16. 设计更改密码格式
- 17. Rails 3设计手动更改密码
- 18. 设计自定义密码更改
- 19. 设计,允许用户更改密码
- 20. 更新密码设计
- 21. 设计更新密码
- 22. 设计密码更新
- 23. 如何更改设计中的重置密码URL RoR
- 24. 在更改为新密码之前确认旧密码codeigniter
- 25. php在更改密码之前验证密码
- 26. 更改帐户设置时检查用户当前的密码
- 27. 更改密码
- 28. 更改密码
- 29. 更改密码
- 30. 如何更改LAMP密码?
当您尝试更改另一个用户的密码时会发生什么?我只是使用我在下面发布的代码做得很好。 – Catfish 2013-03-26 14:56:19