RailsApps / rails-devise-pundit

Rails 5.0 starter app with Devise for authentication and Pundit for authorization.
http://railsapps.github.io/rails-devise-pundit/
483 stars 156 forks source link

undefined method `authorize' when bin/spring is running #10

Closed MarcoBatista closed 10 years ago

MarcoBatista commented 10 years ago

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

DanielKehoe commented 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.

MarcoBatista commented 10 years ago

generate the app.

DanielKehoe commented 10 years ago

What happens when you clone the repo and run $ rspec?

Do you have the file config/initializers/pundit.rb?

binyamindavid commented 10 years ago

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>:
DanielKehoe commented 10 years ago

Is the issue resolved? Or are you still seeing the problem?

DanielKehoe commented 10 years ago

Closing the issue due to lack of response. Please reopen if you still have a problem.

ricardodovalle commented 10 years ago

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.

binyamindavid commented 10 years ago

This happens on the users index. It comes and goes away.

ricardodovalle commented 10 years ago

@vezu, did you solve the issue? Could @MarcoBatista reopen the issue Thanks.

binyamindavid commented 10 years ago

@ricardodovalle i changed my approach, I couldn't fix @DanielKehoe 's code.

ricardodovalle commented 10 years ago

Maybe it is a easier approach keep pundit includes inside application_controller.

gabehoffman commented 10 years ago

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.

DanielKehoe commented 10 years ago

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.

k2xl commented 10 years ago

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.

ndbroadbent commented 10 years ago

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.

ndbroadbent commented 10 years ago

@ricardodovalle's solution works for me (moving the Pundit includes into ApplicationController, so they are properly reloaded)

DanielKehoe commented 10 years ago

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.

navjeetc commented 9 years ago

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?

JeremyEllingham commented 9 years ago

@navjeetc Seconded. Can someone clarify?

vjm commented 9 years ago

They should look something like this: https://gist.github.com/vjm/e7d9dbb7603553bfbd2a

mtrolle commented 9 years ago

@vjm Cool - helped do include Pundit in the ApplicationController - thanks!

cweilemann commented 9 years ago

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?

binyamindavid commented 9 years ago

I think that's the best solution.

serodriguez68 commented 8 years ago

@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?

rlabuonora commented 7 years ago

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?

rlabuonora commented 7 years ago

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.

swiknaba commented 5 years ago

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).