2013-03-26 81 views
4

我使用deviseauthentication和我有每个用户的role,我允许与admin角色的用户创建新的用户,我想管理员用户edit the password为用户剩下的,如果他们忘记了自己的密码。但是我无法在编辑中改变没有当前密码的密码。那么我怎样才能允许管理员用户通过编辑用户密码来改变密码,并像我们为其余的值一样保存。如何在设计中更改没有当前密码的密码?

+0

当您尝试更改另一个用户的密码时会发生什么?我只是使用我在下面发布的代码做得很好。 – Catfish 2013-03-26 14:56:19

回答

2

有一个方法内置设计称为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 
+0

实际上,如果我们想更改其他值而不提供密码,但我们无法更新密码,则可以使用此update_without_password。 – logesh 2013-03-26 14:39:34

+0

我用'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

+0

啊很高兴知道。实际上,我昨天刚刚遇到了一个问题,并且注意到这段代码并未改变用户密码。我会用你的解决方案来解决我的问题。很高兴我们可以一起解决我们的两个问题。 – Catfish 2013-03-28 15:25:40

12

由于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 

这示例用于更新当前用户(包括用户的密码),但您可以对其进行修改以适应您的需求。

+0

是的,这是正确的答案 – 2015-10-08 10:08:25

3
@user.update_attributes(password: params[:user][:password]) 
+0

@Luke什么错了?我不明白你编辑我的答案。你没有在我的答案中提供任何代码。 – chegoon 2014-10-27 08:22:21

+0

如果您在编辑时检查注释,它会显示“添加了代码标签”。这将您的答案中的代码格式化为灰色框,这使得它更易于阅读并且更容易复制/粘贴。 – Luke 2014-10-27 08:25:34

+0

@Luke感谢您的通知:) – chegoon 2014-10-27 08:38:40