Created
October 15, 2014 07:39
-
-
Save iqbalhasnan/460648532a4b697f3d28 to your computer and use it in GitHub Desktop.
Pundit Authorized admin namespace controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Admin::UsersController < ApplicationController | |
before_filter :authenticate_user! | |
before_filter :check_if_admin | |
include Pundit | |
def index | |
@users = User.all | |
end | |
def update | |
if @user.update_attributes(secure_params) | |
redirect_to admin_users_path, :notice => "User updated." | |
else | |
redirect_to admin_users_path, :alert => "Unable to update user." | |
end | |
end | |
def destroy | |
@user.destroy | |
redirect_to admin_users_path, :notice => "User deleted." | |
end | |
private | |
def secure_params | |
params.require(:user).permit(:role) | |
end | |
#DRY | |
def load_user | |
@user = User.find(params[:id]) | |
end | |
def check_if_admin | |
raise Pundit::NotAuthorizedError unless current_user.admin? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment