Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pundit Headless Policy

I'm using pundit for access control in the admin section of my app. I have a dashboards controller that looks like this:

class Admin::DashboardsController < AdminController
  def index
    @total_revenue = Order.total_revenue
    authorize :dashboards, :index?
  end

  ...

end

and a policy that looks like this:

class DashboardPolicy < Struct.new(:user, :dashboard)
  def index?
    true
  end
end

When I try to access /admin/dashboards/ I get a Pundit::NotDefinedError, unable to find policy SymbolPolicy for dashboards

I've also tried namespacing the policy and got the same error.

like image 378
Edward Loveall Avatar asked Aug 05 '14 14:08

Edward Loveall


1 Answers

jizak's answer did not work for me, I found following solution for headless name-spaced policies, the trick being with the [:admin, :policy] first argument.

  class Admin::HomeController < AdminController
    def dashboard
      authorize [:admin, :home], :dashboard?
    end
  end

And then for the policy:

Admin::HomePolicy < AdminPolicy
  def dashboard?
    return false unless user && user.admin?
    true
  end
end
like image 191
tombroomfield Avatar answered Sep 18 '22 12:09

tombroomfield