0

I am getting the following error in Rails:

undefined local variable or method `current_user' for #<UserController:0x0000000458d708> Did you mean? @current_user

The code fragment corresponding to that error is:

authorize @current_user

As you can see, I clearly mean @current_user like the error message suggests, and I also use @current_user like the error message suggests. Why is Rails thinking I mean a local unexisting variable when I expect it to be global? The authorize method is from Pundit.

I added a print statement to verify if the global variable exists, and the following code effectively prints out a valid User:

p @current_user
authorize @current_user
Pavan
  • 33,316
  • 7
  • 50
  • 76
Pieter De Clercq
  • 1,951
  • 1
  • 17
  • 29

1 Answers1

2

To use Pundit, you must define current_user (or pundit_user): https://github.com/elabs/pundit/blob/master/lib/pundit.rb#L270

When you do authorize @current_user, that is assuring that the current_user can perform actions on @current_user using the UserPolicy.

Logan Serman
  • 29,447
  • 27
  • 102
  • 141