Question

I'm getting undefined method 'authorize_actions_for' in the controller when using rails-api with the Authority gem. Any ideas what I need to include? Here's my code:

Gemfile:

...
gem 'authority', '~> 2.9.0'
gem 'rails-api', '~> 0.1.0'
...

app/authorizers/session_authorizer.rb:

class SessionAuthorizer < ApplicationAuthorizer
  def deletable_by?(user)
    user.sessions.include?(resource)
  end
end

app/controllers/v1/sessions_controller.rb:

class V1::SessionsController < ApplicationController
  authorize_actions_for Session
  ...
end
Was it helpful?

Solution

Include Authority::Controller

As we discussed on Github, authorize_actions_for is defined in Authority::Controller.

In a normal Rails app, that module gets included into ActionController::Base by Authority's railtie.rb. Apparently the railtie isn't being required when using rails-api (maybe the Rails constant isn't defined?).

You can include the appropriate module yourself, though:

class ApplicationController < ActionController::Base
  include Authority::Controller
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top