Created
August 19, 2010 15:46
-
-
Save redtettemer/538197 to your computer and use it in GitHub Desktop.
This file contains 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
# 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