CanCan: Load and authorize resource under certain circumstances only
You might want to avoid overhead of putting
load_and_authorize_resource
in every controller that you want to authorize using CanCan by putting it inside ApplicationController
class ApplicationController < ActionController::Base
load_and_authorize_resource
end
Unfortunately, this might cause your SessionsController
(being used by Devise for example) and other controllers to break in some cases, e.g. on :destroy
action.
Why not to try load_and_authorize_resource
under certain circumstances only?
This might really help you:
class ApplicationController < ActionController::Base
load_and_authorize_resource if: lambda { |controller| [:problems, :analyses].include? controller.controller_name.to_sym }
end
Written by Maciej Smoliński
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Related Tags
#ruby
#controller
#devise
#application
#sessions
#conditional
#cancan
#load_and_authorize_resource
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#