Reputation: 19505
The Devise README doesn't mention anything about @current_user
, and I don't see any reference to it in Devise's source code - nor in my app's source code (grep -R "@current_user" app/
) - yet I am able to use @current_user
in my controllers. Why is that?
Upvotes: 1
Views: 116
Reputation: 53038
When you create a Devise model, say User
, then Devise generates a helper method for you dynamically, named current_user
. This method returns the value of instance variable @current_user
. By using @current_user
you are just directly accessing the instance variable instead of via current_user
method. So, you see why the values are the same.
In Devise, the implementation of the current_#{mapping}
helper method is as below:
def current_#{mapping}
@current_#{mapping} ||= warden.authenticate(scope: :#{mapping})
end
where mapping
would be replaced by your Devise model name, which in your case is user
.
Refer to current_#{mapping} method definition in Devise source code.
Upvotes: 3