Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

CanCan 2.0 Aliases

Will Gray edited this page Oct 22, 2013 · 1 revision

Actions

  • :read => [:index, :show]
  • :create => [:new]
  • :update => [:edit]
  • :destroy => [:delete]
  • :access is an alias for all actions

You can add additional action aliases by calling alias_action from the Ability class initialize method. For instance, to add the search and query actions to the read alias: alias_action(:search, :query, :to => :read)

Subject

  • :all is an alias for all subjects

You can add additional subject aliases by calling alias_subject from the Ability class initialize method. For instance, to add a group of API controllers as a single subject: alias_subject(:'api/users', :'api/locations', :to => :api)

Notes

  • :all and :access seem to be 'special' in that they are not listed when you ask an instance of the Ability class for its aliases via ability.aliases

  • You can check the currently defined aliases in rails console by initializing the ability class with something like a = Ability.new(User.first) and then running a.aliases.

  • When defining abilities or setting aliases for namespaced controllers, you must use a quoted symbol such as :'api/users'. A simple string ('api/users') will not work.