4

私はdeviseforauthenticationを使用しておりrole、各ユーザーには for があり、adminロールを持つユーザーが新しいユーザーを作成edit the passwordできるようにしています。パスワードを忘れた場合は、管理者ユーザーに残りのユーザーを作成してもらいます。しかし、現在のパスワードを編集していないとパスワードを変更できません。では、管理者ユーザーがユーザーのパスワードを編集してパスワードを変更し、残りの値と同じように保存できるようにするにはどうすればよいでしょうか。

4

3 に答える 3

2

と呼ばれる工夫するために組み込まれたメソッドがありupdate_without_passwordます。

update メソッドで使用しているものは次のとおりです。

 # 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
于 2013-03-26T14:00:37.387 に答える