Skip to content

Instantly share code, notes, and snippets.

@sergio-fry
Created July 1, 2010 14:00
Show Gist options
  • Save sergio-fry/459998 to your computer and use it in GitHub Desktop.
Save sergio-fry/459998 to your computer and use it in GitHub Desktop.
inherit_resources with cancan
class ApplicationController < ActionController::Base
# Add this to your application_controller
def self.authorized_inherited_resources
inherit_resources
before_filter :authorize_resource_with_cancan
define_method(:authorize_resource_with_cancan) do
case action_name.to_sym
when :new, :create
authorize!(action_name.to_sym, build_resource)
when :show, :edit, :destroy
authorize!(action_name.to_sym, resource)
else
authorize!(action_name.to_sym, resource_class)
end
end
end
end
@NaN1488
Copy link

NaN1488 commented Jun 14, 2013

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment