5
votes

I've installed the gem Pundit in my Rails app, and have followed the instructions in the README carefully.

However, when I use authorize in any of my controllers, I get the error message "undefined method 'authorize' for .

Also, when I try to use "policy" in a view, I get the error "undefined method 'policy'".

It's as if Pundit weren't installed at all!

I've included Pundit in my Application Controller.

I have an ApplicationPolicy file with all of my policies, then policy files for each type of record that inherit from the Application Policy.

For some reason, I can't paste any of my code into this question, so I'm hoping I've included enough information!

I'd appreciate any thoughts.

2
Pundit is "just plain Ruby," so including it in your Application Controller should be all that you need. Are you sure that you spelled everything correctly? Is include Pundit the first line below the ApplicationController declaration. Also, are your controllers inheriting from the ApplicationController?nikkon226
Thank you! These thoughts were helpful.Ellen W
By chance, are you using Clearance for authentication? If so, Devise uses a method called authorize that conflicts with Pundit. In my application controller, I have this alias_method :require_login, :authorize and the next line is my include Pundit. Also, ensure that you are calling authorize on an instance of a model (i.e., authorize @my_var)craig.kaminsky
When used in a Rails Engine, we had to require 'pundit' in the namespaced ApplicationControllerGregor

2 Answers

3
votes

I have recently used pundit and it worked like a charm. your error messages makes me infer that you've not run install rails g pundit:install or your policies file are not in correct directories.

or if they are then Just restarting the server should fix this. Hopefully.

0
votes

You need to generate a default policy file for your application with the pundit:install generator.

rails g pundit:install
  create  app/policies/application_policy.rb