I am attempting to use pundit to check for an authorized user on a class in a controller. The current code I have is:
controllers/contact_mes_controller.rb
def index
authorize ContactMe
@contact_mes = ContactMe.search(params.fetch(:q, "*"), page: params[:page], per_page: 10) #elasticsearch
end
policies/contact_me_policy.rb
class ContactMePolicy < ApplicationPolicy
def index?
user.admin?
end
end
The application policy is an untouched generated policy from the pundit gem.
When I run my feature specs for the index action, I get the following error:
1) ContactMesController GET index sets @contact_mes
Failure/Error: authorize ContactMe
Pundit::NotDefinedError:
unable to find policy `ContactMePolicy` for `ContactMe(id: integer, name: string, email: string, body: text, created_at: datetime, updated_at: date time)`
Unless I read the document wrong, I was under the impression that I was able to check authorization on a class when I didn't have a specific record to check against. Can anyone give me an idea of what I missed here?
Thanks
Something like this as suggested in the installation guide?
After generating your application policy, restart the Rails server so that Rails can pick up any classes in the new app/policies/ directory.
Hi,
I'm getting this error when I run my rspec tests. As far as I am aware there is nothing to restart.
Sent from my iPhone
On Feb 25, 2016, at 15:22, Nikolay Bekirov [email protected] wrote:
Something like this as suggested in the installation guide?
After generating your application policy, restart the Rails server so that Rails can pick up any classes in the new app/policies/ directory.
—
Reply to this email directly or view it on GitHub.
This is the closest I could get to reproducing your issue:
vagrant@rails-dev-box:/vagrant/pundit365$ bundle exec rspec
.
Finished in 0.09088 seconds (files took 2.88 seconds to load)
1 example, 0 failures
vagrant@rails-dev-box:/vagrant/pundit365$ mv app/policies/contact_me_policy.rb app/policies/contact_me_policy2.rb
vagrant@rails-dev-box:/vagrant/pundit365$ bundle exec rspec
F
Failures:
1) ContactMesController renders the index template
Failure/Error: authorize ContactMe
Pundit::NotDefinedError:
unable to find policy `ContactMePolicy` for `ContactMe(id: integer, name: string, created_at: datetime, updated_at: datetime)`
# /var/lib/gems/2.3.0/gems/pundit-1.1.0/lib/pundit/policy_finder.rb:59:in `policy!'
# /var/lib/gems/2.3.0/gems/pundit-1.1.0/lib/pundit.rb:112:in `policy!'
# /var/lib/gems/2.3.0/gems/pundit-1.1.0/lib/pundit.rb:235:in `policy'
# /var/lib/gems/2.3.0/gems/pundit-1.1.0/lib/pundit.rb:194:in `authorize'
# ./app/controllers/contact_mes_controller.rb:7:in `index'
# ./spec/controllers/contact_mes_controller_spec.rb:5:in `block (2 levels) in <top (required)>'
Finished in 0.02502 seconds (files took 2.92 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/controllers/contact_mes_controller_spec.rb:4 # ContactMesController renders the index template
Files:
app/models/contact_me.rbclass ContactMe < ActiveRecord::Base
end
app/controllers/contact_mes_controller.rbclass ContactMesController < ApplicationController
def index
authorize ContactMe
@contact_mes = ContactMe.all
end
end
app/policies/contact_me_policy.rbclass ContactMePolicy < ApplicationPolicy
class Scope < Scope
def resolve
scope
end
end
def index?
true
end
end
spec/controllers/contact_mes_controller_spec.rbrequire 'rails_helper'
RSpec.describe ContactMesController, type: :controller do
it "renders the index template" do
get :index
expect(response).to render_template("index")
end
end
Please check again your policies path and file access or post any differences that may be relevant.
OK. I will check what you have against what I have and see if I can figure out. I will respond once I do so.
Thanks for helping me with this.
I should have looked much much much more closely. app/policies/contact_me_policy.rb was actually app/policies/contact_me.policy.rb
I should have done a better job of debugging this. I thought I looked at all of the simple mistakes.
I apologize.
Most helpful comment
Something like this as suggested in the installation guide?