Skip to content

Instantly share code, notes, and snippets.

@redtettemer
Created August 19, 2010 15:46
Show Gist options
  • Save redtettemer/538197 to your computer and use it in GitHub Desktop.
Save redtettemer/538197 to your computer and use it in GitHub Desktop.
# My user model
class User < ActiveRecord::Base
acts_as_authentic
ROLES = %w[admin editor author banned]
end
# My ability class, used by CanCan for defining permissions
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new #guest user
if user.admin?
can :manage, :all
else
can :read, :all
end
end
end
#In my application controller
rescue_from CanCan::AccessDenied do |exception|
flash[:error] = exception.message
redirect_to login_url
end
# And the first line in my posts controller
class PostsController < ApplicationController
load_and_authorize_resource
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment