module CanCan::ControllerAdditions::ClassMethods
Public Instance Methods
Sets up a before filter which loads the appropriate 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 Article.new(params) depending upon the action. It does nothing for the “index” action.
Call this method directly on the controller class.
class BooksController < ApplicationController load_resource end
A resource is not loaded if the instance variable is already set. This makes it easy to override the behavior through a before_filter on certain actions.
class BooksController < ApplicationController before_filter :find_book_by_permalink, :only => :show load_resource private def find_book_by_permalink @book = Book.find_by_permalink!(params[:id) end end
See #load_and_authorize_resource to automatically authorize the resource too.
Options:
- :
only
-
Only applies before filter to given actions.
- :
except
-
Does not apply before filter to given actions.
- :
nested
-
Specify which resource this is nested under.
load_resource :nested => :author
Deep nesting can be defined in an array.
load_resource :nested => [:publisher, :author]
- :
name
-
The name of the resource if it cannot be determined from controller (string or symbol).
load_resource :name => :article
- :
resource
-
The class to use for the model (string or constant).
- :
collection
-
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 an :id
is present inparams
.load_resource :collection => [:sort, :list]
- :
new
-
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
# File lib/cancan/controller_additions.rb, line 84 def load_resource(options = {}) ResourceAuthorization.add_before_filter(self, :load_resource, options) end