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.
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
Most helpful comment
I had a similar issue. If you have a
Admin::Usermodel you may not use a simple symbol such asauthorize :userbecause pundits policy finder will turn this intoUser(namespace missing) usingobject.to_s.camelize.Check your parameter of the
authorizecall. You either have to provide your class (or an instance of it) such asauthorize Admin::Useror you need to provide a prefixed symbol such asauthorize :"admin/user".