Closed MarcoBatista closed 10 years ago
Did you generate the app using Rails Composer or did you clone from the repository?
You should be able to clone from the repository and run $ rspec
to confirm the app will run. Be sure you've run $ bundle install
to make sure the gems are loaded.
generate the app.
What happens when you clone the repo and run $ rspec
?
Do you have the file config/initializers/pundit.rb?
I cloned this app. This error is not persistent. When you restart our app, goes away
Started GET "/users" for 127.0.0.1 at 2014-07-02 16:07:35 +0200
Processing by UsersController#index as HTML
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
User Load (0.3ms) SELECT "users".* FROM "users"
Completed 500 Internal Server Error in 12ms
NoMethodError - undefined method `authorize' for #<UsersController:0x0000010a1535a0>:
Is the issue resolved? Or are you still seeing the problem?
Closing the issue due to lack of response. Please reopen if you still have a problem.
Hi @DanielKehoe and @MarcoBatista
I have same issue.
The issue is with autoreload, when I modified any view file of an action that has authorize
method, i got the error:
NoMethodError - undefined method `authorize' for #<PeopleController:0x007fbc55d4d820>:in `show'
If I restart the app, it works fine while any file modification.
The only solution that I founded, It have been move the content of /initializers/pundit.rb
to application_controller.rb
(removing included do ...
method).
Thanks for any better solution.
This happens on the users index. It comes and goes away.
@vezu, did you solve the issue? Could @MarcoBatista reopen the issue Thanks.
@ricardodovalle i changed my approach, I couldn't fix @DanielKehoe 's code.
Maybe it is a easier approach keep pundit includes inside application_controller.
so, I ran into this error today when I was seeding some users to test with. I ran one as
User.create!(name: "Test User", email: "tester@server.com", password: "changeme", password_confirmation: "changeme")
and then I got lazy and I used FactoryGirl for the next one
testuser = FactoryGirl.create(:user, name: "Test User 2")
When I removed the FactoryGirl user, everything was okay again. Not exactly sure what's up with that. But it might help someone.
The inconsistent behavior is due to Spring, the Rails application preloader. Spring keeps your application running in the background (the intent is to eliminate time needed to start the application during development).
If you change the Pundit policy file and Spring is running, you'll get an error undefined method `authorize'.
You can check if Spring is running:
$ bin/spring status
You can stop Spring:
$ bin/spring stop
When you get the error undefined method `authorize' stop Spring with bin/spring stop
and the problem will be resolved.
I'm closing the issue but feel free to comment.
I just encountered the issue today too (just installed via RailsComposer). Seems like the bin/spring stop doesn't work for me. Restarting the rails app is the only thing that does it seems.
I've been running into this for the last few weeks.
When you get the error undefined method `authorize' stop Spring with bin/spring stop and the problem will be resolved.
I would like to find a better solution, It's very annoying to have to do this every time I make a change.
@ricardodovalle's solution works for me (moving the Pundit includes into ApplicationController, so they are properly reloaded)
If you move the Pundit includes into the ApplicationController, move them back when you are done with development so you get the benefit of faster performance.
What exactly needs to be moved from the pundit initializer and to where exactly in Application controller? Can you somebody post the final code for both initializer and Application controller?
@navjeetc Seconded. Can someone clarify?
They should look something like this: https://gist.github.com/vjm/e7d9dbb7603553bfbd2a
@vjm Cool - helped do include Pundit in the ApplicationController - thanks!
I'm seeing this running rspec. Stopping and restarting spring seems to work for one round of spec tests... So, does this mean that the permanent solution is to move the pundit initializer code to the ApplicationController for both development and test?
I think that's the best solution.
@vjm 's solution works fine. However I would like to know why are there performance benefits by using an initializer instead of just moving all to the application controller permanently?
Same problem here. I tried restarting the server, stopping spring, and moving the code from initializers/pundit.rb to the application controller. It does work fine on Heroku, but I get that error on my local machine.
Any ideas?
Ok, I just had to put the line:
include Pundit
at the top of ApplicationController as per the first answer to this question on Stack Overflow and it worked.
This issue occurred for me with rails 5.1.6 a while ago. Upgrading to 5.2.1 solved the problem (well, now you probably want to try 5.2.2).
I installed railsApps, rails+devise+pundit, after register an user and when list all users, I have this error.
undefined method `authorize' for #<UsersController
def index @users = User.all -> authorize User <- end