Method: CanCan::ControllerAdditions::ClassMethods#load_resource

Defined in:

#load_resource(*args) ⇒ Object

Sets up a before filter which loads the model resource into an instance variable. For example, given an ArticlesController it will load the current article into the @article instance variable. It does this by either calling Article.find(params) or depending upon the action. The index action will automatically set @articles to Article.accessible_by(current_ability).

If a conditions hash is used in the Ability, the new and create actions will set the initial attributes based on these conditions. This way these actions will satisfy the ability restrictions.

Call this method directly on the controller class.

class BooksController < ApplicationController

A resource is not loaded if the instance variable is already set. This makes it easy to override the behavior through a before_action on certain actions.

class BooksController < ApplicationController
  before_action :find_book_by_permalink, :only => :show


  def find_book_by_permalink
    @book = Book.find_by_permalink!(params[:id])

If a name is provided which does not match the controller it assumes it is a parent resource. Child resources can then be loaded through it.

class BooksController < ApplicationController
  load_resource :author
  load_resource :book, :through => :author

Here the author resource will be loaded before each action using params. The book resource will then be loaded through the @author instance variable.

That first argument is optional and will default to the singular name of the controller. A hash of options (see below) can also be passed to this method to further customize it.

See load_and_authorize_resource to automatically authorize the resource too.



Only applies before filter to given actions.


Does not apply before filter to given actions.


Load this resource through another one. This should match the name of the parent instance variable or method.


The name of the association to fetch the child records through the parent resource. This is normally not needed because it defaults to the pluralized resource name.


Pass true to allow this resource to be loaded directly when parent is nil. Defaults to false.


Pass true if this is a singleton resource through a has_one association.


True or false depending on if the resource is considered a parent resource. This defaults to true if a resource name is given which does not match the controller.


The class to use for the model (string or constant).


The name of the instance variable to load the resource into.


Find using a different attribute other than id. For example.

load_resource :find_by => :permalink # will use find_by_permalink!(params[:id])

Find using a param key other than :id. For example:

load_resource :id_param => :url # will use find(params[:url])

Specify which actions are resource collection actions in addition to :index. This is usually not necessary because it will try to guess depending on if the id param is present.

load_resource :collection => [:sort, :list]

Specify which actions are new resource actions in addition to :new and :create. Pass an action name into here if you would like to build a new resource instead of fetch one.

load_resource :new => :build

Passing true will use prepend_before_action instead of a normal before_action.

# File 'lib/cancan/controller_additions.rb', line 119

def load_resource(*args)
  cancan_resource_class.add_before_action(self, :load_resource, *args)