I’ve installed login_sugar in my app. In my Appliation controller I
have added
include UserSystem
before_filter :authenticate_user
then, in controllers with actions where I don’t need to have
authentication, I’ve added
before_filter :authenticate_user, :except => [:index]
It works wonderfully, with one exception: login_sugar provides an
instance variable @current_user which is handy for doing all sorts of
things on a page with the currently logged in user. @current_user is
initialized in the authenticate_user filter by calling the method
authenticated_user?.
The problem I’m having is that there are pages that don’t require
authentication where I still want to have @current_user available to
me. For example, on the support page: I don’t require that you be
logged into request support, but if you are logged in then I can
render the page more appropriately for your needs.
So, the easy way to solve this problem is like so:
def index
authenticated_user?
end
but, the problem with that is that it’s not DRY. In other words, for
every controller where I use :except on an action I also have to add
the call to authenticated_user? to the action. What I thought I would
do is call authenticated_user? from the Application controller, like
so:
include UserSystem
before_filter :authenticate_user
authenticated_user?
which would result in @current_user being initialized on every page
via authenticated_user?. However, that does not work; the result is a
500 error with the following message:
undefined method `authenticated_user?’ for
ApplicationController:Class
That being said, two questions:
-
I don’t get it…authenticated_user? has been mixed-in to the
Application controller via the include UserSystem statement. Why is
it not available? -
What is the “proper” solution to my original problem of having
@current_user initialized on each page?
Thanks,
Scott