2017-03-18 347 views
3

我的用戶控制器出現問題。目前,管理員用戶可以自行刪除,其他用戶不能刪除自己。另外,每個用戶都可以自己編輯。管理員用戶允許刪除和編輯其他用戶

但是,我想要一個管理員用戶刪除和編輯自己和其他人。我如何編輯用戶控制器?

用戶控制器:

class UsersController < ApplicationController 
    before_action :logged_in_user, only: [:index, :edit, :update, :destroy, 
             :following, :followers] 
    before_action :correct_user, only: [:edit, :update] 
    before_action :admin_user,  only: :destroy 

    def edit 
    @user = User.find(params[:id]) 
    end 

    def destroy 
    User.find(params[:id]).destroy 
    flash[:success] = "Nutzer gelöscht" 
    redirect_to users_url 
    end 


    private 

    def user_params 
    params.require(:user).permit(:name, :capacity, :resource_id, :email, :password, 
           :password_confirmation) 
    end 

    # Before filters 

    # Confirms the correct user. 
    def correct_user 
    @user = User.find(params[:id]) 
    redirect_to(root_url) unless current_user?(@user) 
    end 

    # Confirms an admin user. 
    def admin_user 
    redirect_to(root_url) unless current_user.admin? 
    end 
end 

感謝您的幫助! :)

最佳方面, 菲利普

回答

1

目前,before_action :correct_user, only: [:edit, :update]防止管理員進入編輯頁面。修改correct_user方法來更改此行爲:

# Confirms the correct user. 
    def correct_user 
    @user = User.find(params[:id]) 
    redirect_to(root_url) unless current_user?(@user) || current_user.admin? 
    end 
+0

謝謝!這對我來說足夠了:) – PhillipLuepke

+0

@PhillipLuepke,你的歡迎。如果我的回答確實有幫助,請考慮通過點擊答案左側的複選標記將其標記爲已接受 –

相關問題