Pundit: Undefined method `authorize`

Created on 21 Dec 2014  路  12Comments  路  Source: varvet/pundit

Hello

During development, I often see this error. It goes away when I restart "rails server', but it's pretty annoying. Any suggestions?

thanks.

Most helpful comment

For me, I was missing include Pundit at the top of my controller. Adding that line fixed it.

All 12 comments

One might experience this error with any new gem that's added to the Gemfile, you'll need to restart $ rails serverafter$ bundle install. However, after that the gem should be properly loaded. If this error persist, I'd guess there's some issue withbundler` not properly loading gems on your system.

I am not sure that this issue is truly closed.

I am seeing the same behavior as DmitrlySalko. If I modify a policy within my policies directory, the only way to get rid of the _undefined method `authorize' for_ message is to restart the server. This immediately allows for the server to work.

Environment:
Puma Server
Created with Rails Composer

Gemfile:
Using rake 10.4.2
Using i18n 0.7.0
Using json 1.8.2
Using minitest 5.6.0
Using thread_safe 0.3.5
Using tzinfo 1.2.2
Using activesupport 4.2.1
Using builder 3.2.2
Using erubis 2.7.0
Using mini_portile 0.6.2
Using nokogiri 1.6.6.2
Using rails-deprecated_sanitizer 1.0.3
Using rails-dom-testing 1.0.6
Using loofah 2.0.1
Using rails-html-sanitizer 1.0.2
Using actionview 4.2.1
Using rack 1.6.0
Using rack-test 0.6.3
Using actionpack 4.2.1
Using globalid 0.3.5
Using activejob 4.2.1
Using mime-types 2.4.3
Using mail 2.6.3
Using actionmailer 4.2.1
Using activemodel 4.2.1
Using arel 6.0.0
Using activerecord 4.2.1
Using addressable 2.3.8
Using ast 2.0.0
Using parser 2.2.0.3
Using astrolabe 1.3.0
Using execjs 2.5.2
Using autoprefixer-rails 5.1.9
Using bcrypt 3.1.10
Using coderay 1.1.0
Using better_errors 2.1.1
Using debug_inspector 0.0.2
Using binding_of_caller 0.7.2
Using bootstrap-filestyle-rails 1.1.2
Using sass 3.4.13
Using bootstrap-sass 3.3.4.1
Using columnize 0.9.0
Using byebug 4.0.5
Using xpath 2.0.0
Using capybara 2.4.4
Using ffi 1.9.8
Using childprocess 0.5.6
Using climate_control 0.0.3
Using cocaine 0.5.7
Using cocoon 1.2.6
Using coffee-script-source 1.9.1.1
Using coffee-script 2.4.1
Using thor 0.19.1
Using railties 4.2.1
Using coffee-rails 4.1.0
Using database_cleaner 1.4.1
Using orm_adapter 0.5.0
Using responders 2.1.0
Using warden 1.2.3
Using devise 3.4.1
Using diff-lcs 1.2.5
Using factory_girl 4.5.0
Using factory_girl_rails 4.5.0
Using faker 1.4.3
Using figaro 1.1.0
Using font-awesome-rails 4.3.0.0
Using tilt 1.4.1
Using haml 4.0.6
Using sexp_processor 4.5.0
Using ruby_parser 3.6.6
Using html2haml 2.0.0
Using haml-rails 0.9.0
Using high_voltage 2.2.1
Using interception 0.5
Using multi_json 1.11.0
Using jbuilder 2.2.13
Using jquery-rails 4.0.3
Using sprockets 3.0.0
Using sprockets-rails 2.2.4
Using sass-rails 5.0.3
Using jquery-datatables-rails 3.2.0
Using jquery-ui-rails 5.0.3
Using kaminari 0.16.3
Using launchy 2.4.3
Using method_source 0.8.2
Using paperclip 4.2.1
Using polyamorous 1.2.0
Using powerpack 0.1.0
Using slop 3.6.0
Using pry 0.10.1
Using pry-rails 0.3.4
Using pry-rescue 1.4.1
Using puma 2.11.2
Using pundit 0.3.0
Using quiet_assets 1.1.0
Using bundler 1.6.1
Using rails 4.2.1
Using rails4-autocomplete 1.1.1
Using rails_layout 1.0.25
Using rainbow 2.0.0
Using ransack 1.6.6
Using rspec-support 3.2.2
Using rspec-core 3.2.3
Using rspec-expectations 3.2.1
Using rspec-mocks 3.2.1
Using rspec-rails 3.2.1
Using ruby-progressbar 1.7.5
Using rubocop 0.30.0
Using rubyzip 1.1.7
Using websocket 1.2.1
Using selenium-webdriver 2.45.0
Using simple_form 3.1.0
Using spring 1.3.4
Using spring-commands-rspec 1.0.4
Using sqlite3 1.3.10
Using timeliness 0.3.7
Using turbolinks 2.5.3
Using uglifier 2.7.1
Using upmin-admin 0.1.01
Using validates_timeliness 3.0.14
Using web-console 2.1.2

Steps to recreate:
Ensure that pundit is working properly
View a page that has a policy and uses _authorize @object_ where object is your model
cd to the app/policies directory
touch object_policy.rb
refresh the page
you should receive the same error

It feels as if this may not necessarily be a pundit issue but an issue with how pundit is integrated into the application configuration so that a modified policy is not picked up properly on change.

@achansonjr just tried to replicate this and everything worked just fine. I used a bare Rails app though and you have a _lot_ of gems in there that could be doing something wonky. Maybe try it with a simpler app, or try taking away gems until you find which one breaks it?

Totally understand. This app does have just about everything except the kitchen sink. I will try and investigate to determine what is going on and get back if I find anything. Thanks for the response.

Just a quick note while I was trying something. I am on a different machine that is newer (less rubies, gems, etc) and it is not exhibiting this behavior. (Just pointing out that this machine was recently set up, less development cruft) No idea but if someone else comes looking for something like this, maybe it will help. It is mainly just annoying in development because it requires a restart for every policy change.

@achansonjr Please post here if you find anything. Thank you.

May it be related to spring gem?

p.s. there is a workaround, but I don't really like it.

@DmitriySalko It is possible. However the bundles for the app should be using the same gems, and running with the same processes, so I can't see why it would be working on one but not the other. I think your idea that there is almost too much crap in here to isolate and say its a pundit issue is on the right track. When I get to the other computer tomorrow I will give it a go by turning off spring, or anything related to the continuous development and see if that fixes it.

currently having this issue and comment out the spring gem, now it works...anyone know whats causing this?

For me, I was missing include Pundit at the top of my controller. Adding that line fixed it.

@jasonswett are you using the Spring gems?

@bliaxiong Yes.

fwiw i had to do the same with rails 5.2 and pundit 2.0 - didnt work before doing so.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ucarion picture ucarion  路  4Comments

dowi picture dowi  路  4Comments

jdwolk picture jdwolk  路  3Comments

guzart picture guzart  路  4Comments

AngelVillanueva picture AngelVillanueva  路  3Comments