I have an Rspec (2.14) test that fails when I try using the policy() helper in my view. The spec fails saying the policy method is not available. When I view the page in my browser it works fine and I don't get an error. I've never seen something like this happen before where a helper was not available in a spec, so I figured I'd open a ticket in case someone had run into this before.
The error:
ActionView::Template::Error:
undefined method `policy' for #<#<Class:0x0000000ab4ae60>:0x0000000ab4a4d8>
Code from my view when it's failing:
<% if policy(Subscription::Free.new).create? %>
If I update my template to the following it works in both the browser and specs, so I have a workaround, but it's odd.
<% if SubscriptionPolicy.new(current_user, Subscription::Free.new).create? %>
Is Pundit included in the ApplicationController? (See the Readme)
class ApplicationController < ActionController::Base
include Pundit
...
end
It's in my AdminController which subclasses ApplicationController.
This was me being a total idiot. I was sharing a partial with a view that was not subclassing the ApplicationController. :grimacing:
I am having this same issue but on a controller that inherits from ApplicationController and in the ApplicationController I have include Pundit. The only other thing I can add is I am using Draper and the policy method is being called from inside a decorate method on the project class.
@jaykilleen have you found a solution? I have the same issue when i use shared partials.
@jaykilleen See this gist:https://gist.github.com/mkhairi/5148a107d2ed2d8d797f PunditViewPolicy did the trick...
I also have this problem and would like to _not_ have to use a workaround.
Having the same issue as @jaykilleen and @TheNeikos in the Draper decorators.
undefined method `policy' for #UserDecorator:0x007f80a3e31dd8
Frustrating!
Running into this with a view spec on a new Rails 5 project, trying to determine what's different now.
Still nothing on this and https://github.com/elabs/pundit/issues/339 was also closed. Is nobody bothering with view specs at this point?
I'm bothering with view specs and have the same issue. New Rails 5 project, current versions of All The Gems.
Still failing on rails 5.1
Same here
Just to leave a note: https://stackoverflow.com/questions/29298184/why-pundit-cant-find-policies-in-controller-tests/38236956#38236956 may fix it.
same here
Most helpful comment
Running into this with a view spec on a new Rails 5 project, trying to determine what's different now.