Skip to content

Instantly share code, notes, and snippets.

@iqbalhasnan
Created October 15, 2014 07:39
Show Gist options
  • Save iqbalhasnan/460648532a4b697f3d28 to your computer and use it in GitHub Desktop.
Save iqbalhasnan/460648532a4b697f3d28 to your computer and use it in GitHub Desktop.
Pundit Authorized admin namespace controller
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