# Login\_sugar: how to initialize @current\_user on non-authenticated pages?

**URL:** <https://www.ruby-forum.com/t/login-sugar-how-to-initialize-current-user-on-non-authenticated-pages/116158>\
**Category:** Rails\
**Created:** [October 3, 2007, 12:53am UTC](https://www.ruby-forum.com/t/login-sugar-how-to-initialize-current-user-on-non-authenticated-pages/116158 "2007-10-03T00:53:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dark-omen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c68b51/32.png) [@dark-omen](https://www.ruby-forum.com/u/dark-omen)\
**Post date:** [October 3, 2007, 12:53am UTC](https://www.ruby-forum.com/t/login-sugar-how-to-initialize-current-user-on-non-authenticated-pages/116158/1 "2007-10-03T00:53:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dark-omen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c68b51/32.png) [@dark-omen](https://www.ruby-forum.com/u/dark-omen)\
**Post date:** [October 3, 2007, 3:55pm UTC](https://www.ruby-forum.com/t/login-sugar-how-to-initialize-current-user-on-non-authenticated-pages/116158/2 "2007-10-03T15:55:22Z")

</div>

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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](http://www.jeremymcanally.com/)

Ruby in Practice:

> **[Ruby in Practice](https://www.manning.com/books/ruby-in-practice)**
>
> Like Ruby itself, Ruby in Practice will make you more productive. The book shows you practical techniques and strategies for small projects and large-scale environments. A cookbook-style reference, it gives you concrete examples of systems...

My free Ruby e-book:  
[http://www.humblelittlerubybook.com/book/](http://www.humblelittlerubybook.com/book/)

My blogs:

> **[Site not found · DreamHost](http://www.mrneighborly.com/)**
>
> The owner of this domain has not yet uploaded their website.

[http://www.rubyinpractice.com/](http://www.rubyinpractice.com/)
