Login_sugar: how to initialize @current_user on non-authenticated pages?

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:

  1. 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?

  2. What is the “proper” solution to my original problem of having
    @current_user initialized on each page?

Thanks,

Scott

You should probably use logged_in? to test whether or not someone is
logged in, and then use current_user contingent upon the results of
that.

–Jeremy

On 10/2/07, Scott [email protected] wrote:

before_filter :authenticate_user, :except => [:index]
logged into request support, but if you are logged in then I can
the call to authenticated_user? to the action. What I thought I would

@current_user initialized on each page?

Thanks,

Scott


http://www.jeremymcanally.com/

Ruby in Practice:

My free Ruby e-book:
http://www.humblelittlerubybook.com/book/

My blogs:
http://www.mrneighborly.com/
http://www.rubyinpractice.com/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs