CanCanCan centralizes ability rules that decide which resources a user may access, with Rails helpers to load models and authorize REST controllers automatically.
Overview
CanCanCan provides an authorization layer for Ruby and Rails applications. Permissions live in Ability classes instead of being duplicated across controllers, views, and queries, and helpers like can? and cannot? gate UI and logic consistently.
Beyond checks, it can fetch only records a user may read by applying rules directly to ActiveRecord scopes when configured. Controller helpers such as authorize! raise on violations, while load_and_authorize_resource automates RESTful resource loading.
Installation is a standard Gemfile entry with a generator for Ability stubs. The project tests across multiple Rails versions via Appraisal and documents guides, screencasts, and Stack Overflow support channels.
It complements authentication stacks by focusing purely on access rules.
Key features
- Central Ability rule files
- can and cannot helpers
- Scoped model fetching
- authorize! enforcement
- load_and_authorize_resource
- Rails REST integration
- Works outside Rails models
Use cases
- Role-based Rails permissions
- Shared authorization logic
- Query-scoped data access
- Controller action gating