| Module | CanCan::ControllerAdditions::ClassMethods |
| In: |
lib/cancan/controller_additions.rb
|
Sets up a before filter which authorizes the current resource using the instance variable. For example, if you have an ArticlesController it will check the @article instance variable and ensure the user can perform the current action on it. Under the hood it is doing something like the following.
authorize!(params[:action].to_sym, @article || Article)
Call this method directly on the controller class.
class BooksController < ApplicationController
authorize_resource
end
See load_and_authorize_resource to automatically load the resource too.
Options:
load_resource :name => :article
Sets up a before filter which loads and authorizes the current resource. This performs both load_resource and authorize_resource and accepts the same arguments. See those methods for details.
class BooksController < ApplicationController
load_and_authorize_resource
end
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[:id]) or Article.new(params[:article]) 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:
load_resource :nested => :author
Deep nesting can be defined in an array.
load_resource :nested => [:publisher, :author]
load_resource :name => :article
load_resource :collection => [:sort, :list]
load_resource :new => :build