John
John

Reputation: 13705

undefined method `user_signed_in?' for ApplicationController:Class, Devise

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?

Upvotes: 2

Views: 4861

Answers (1)

dnatoli
dnatoli

Reputation: 7012

He's saying call the function using the controller class definition itself, not an instance of the controller class. In the code self == ApplicationController he is comparing self to the reference of the class definition ApplicationController rather than to a specific instance (or object) of ApplicationController.

In this case, the post is essentially saying to call the debugger function 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:

class ApplicationController < ActionController::Base
  protect_from_forgery

  def find_user_name
     if user_signed_in?
        return user.user_name
     end
  end

end

Upvotes: 2

Related Questions