Search code examples
ruby-on-railsrubyruby-on-rails-4cancangrape-api

How to use CanCan with Grape?


I wanna use CanCan for authorization in my API. How can I use, for example, the authorize! method from Grape::API module? For now, when I'm trying to use it, it returns me this: undefined method 'authorize!' for #<Grape::Endpoint:0xca39664>


Solution

  • Ok, authorize! is a addition for ActionController::Base, see this source

    you can define own Grape helper:

    class API < Grape::API
      helpers do
       def authorize!(*args)
         # you already implement current_user helper :)
         ::Ability.new(current_user).authorize!(*args)
       end
      end
    end
    

    or use module helper: helpers CanCan::ControllerAdditions (btw, I don't think is a good idea)