2017-03-18 244 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,你的欢迎。如果我的回答确实有帮助,请考虑通过点击答案左侧的复选标记将其标记为已接受 –

相关问题