pundit: Unable to access namespaced policy in view
Given a TagsController and an Admin::TagsController each with a corresponding policy, how does one access the admin policy from a view? I’ve got a global nav link I’d like to only expose to admins.
policy(Tag).index? references the non-Admin namespace policy
I assumed something like this would work: policy(Admin::Tag).index? However, Admin::Tag is not defined and thus, it throws an error.
Ideally, I’d do something like so: policy(Tag, namespace: 'admin').index?
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Comments: 30 (5 by maintainers)
Commits related to this issue
- Add support for custom policy namespace lookup from controllers and views. Fixes #178 — committed to DanOlson/pundit by DanOlson 10 years ago
- Revert namespaces — committed to varvet/pundit by jnicklas 10 years ago
My 2 cents are that I’m convinced that authorization should be tied to controllers and not models. To that end, namespacing would be nice.
However if the Pundit philosophy is of a model based authorization, then it’s almost definitely a bad idea to add namespacing.
As a potential work around for those trying to get namespaced policies in views, you can do something like:
If you have a
Foo::BarPolicyand you’re trying to access theshow?methodYou can even wrap this into a helper for easier use
Then used in a view like: