pundit cant find policy while using namespace

Created on 26 Jan 2014  路  8Comments  路  Source: varvet/pundit

I'm using standard "admin" namespace, yet if i use 'authorize' method in it, pundit still looks for "standard" class.
eg i have admin::userscontroller, and in my policies, directory 'admin' with admin::userpolicy but all the time i get
'unable to find policy UserPolicy' error.

Most helpful comment

I had a similar issue. If you have a Admin::User model you may not use a simple symbol such as authorize :user because pundits policy finder will turn this into User (namespace missing) using object.to_s.camelize.

Check your parameter of the authorize call. You either have to provide your class (or an instance of it) such as authorize Admin::User or you need to provide a prefixed symbol such as authorize :"admin/user".

All 8 comments

Policies follow the namespacing of models, not of controllers, so if you had an Admin::User model, you could use a Admin::UserPolicy policy.

And your user_policy.rb file must be saved in app/policies/admin folder

this is exactly the point, when i have it in folder you mentioned, pundit cant find it. when i just throw it into "policies" it works. i tried to solve it with "require_dependencies" in admin::users_controller pointing onto policies/admin, and it kinda works. kinda, because anytime i make a change in the policy, i get "superclass mismatch for class UserPolicy" and have to restart server.

Actually it worked for me. My previous solution was manually defining policy class in namespaced class

module DepattoModels

  class Activity
    def self.policy_class
      ActivityPolicy
    end
  end
end

Is your policy class definition like this?

class Admin::UserPolicy [....]

Yes

Then... sorry I do not know why it doesn't work :(
I'm currently using pundit 0.2.1

I had a similar issue. If you have a Admin::User model you may not use a simple symbol such as authorize :user because pundits policy finder will turn this into User (namespace missing) using object.to_s.camelize.

Check your parameter of the authorize call. You either have to provide your class (or an instance of it) such as authorize Admin::User or you need to provide a prefixed symbol such as authorize :"admin/user".

This solves my issue. Thanks @jnicklas

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mackshkatz picture mackshkatz  路  4Comments

gabrielecirulli picture gabrielecirulli  路  4Comments

epinault picture epinault  路  6Comments

murdoch picture murdoch  路  6Comments

chevinbrown picture chevinbrown  路  4Comments