The following code:
class ApplicationController < ActionController::Base
protect_from_forgery
if user_signed_in?
end
end
yields this error:
undefined method `user_signed_in?' for ApplicationController:Class, Devise
I looked at this post but I don’t know what he means by instance of the application controller vs. definition of the application controller. I have devise_for :users set up in the config/routes.rb file, am not using clear_helpers, and have :database_authenticatable in my user.rb file. This if statement functions just fine if it’s in an action in a different controller. Why is it not working here? Do I need to pass the devise helper in somehow?
He’s saying call the function using the controller class definition itself, not an instance of the controller class. In the code
self == ApplicationControllerhe is comparingselfto the reference of the class definitionApplicationControllerrather than to a specific instance (or object) ofApplicationController.In this case, the post is essentially saying to call the
debuggerfunction within a method of the controller and not just somewhere in the definition of the controller.As the post mentions, you should be calling user_signed_in? within the context of an instance, not the definition. So put it in a method to be used for a before filter or something. For example: