| Start |
|---|
| Getting started with CanCan CanCan (opens new window) is a a popular authorization library for Ruby on Rails which restricts user access to specific resources. The latest gem (CanCanCan) is a continuation of the dead project CanCan (opens new window). Permissions are defined in the To add authorization support to an app, add the CanCanCan gem to the Then define the ability class: Then check authorization using
Note: This assumes the signed user is provided by the |
| Handling large number of abilities Once the number of abilities definitions start to grow in number, it becomes more and more difficult to handle the Ability file. The first strategy to handle these issue is to move abilities into meaningful methods, as per this example: Once this class grow large enough, you can try breaking it into different classes to handle the different responsibilities like this: and then define those classes as: and so on with |
| Defining abilities Abilities are defined in the |
| Quickly test an ability If you'd like to quickly test if an ability class is giving the correct permissions, you can initialize an ability in the console or on another context with the rails environment loaded, just pass an user instance to test against: More information: https://github.com/ryanb/cancan/wiki/Testing-Abilities (opens new window) RemarksBefore using CanCan don't forget to create Users either by devise gem or manually. To get maximum functionality of CanCan do create an Admin user. |
Read Article
https://develop.horje.com/learn/1434/reference