# ApplicationController

**URL:** <https://discuss.rubyonrails.org/t/applicationcontroller/3974>\
**Category:** rubyonrails-talk\
**Created:** [November 3, 2006, 10:03am UTC](https://discuss.rubyonrails.org/t/applicationcontroller/3974 "2006-11-03T10:03:32Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gustav\_Railist](https://avatars.discourse-cdn.com/v4/letter/g/d2c977/32.png) [@Gustav\_Railist](https://discuss.rubyonrails.org/u/Gustav_Railist)\
**Post date:** [November 3, 2006, 10:03am UTC](https://discuss.rubyonrails.org/t/applicationcontroller/3974/1 "2006-11-03T10:03:32Z")

</div>

Andreas Schneider wrote:

> Hi all,
> 
> perhaps I am on the wrong path - because I come from the ASP world 🙂
> 
> I want that my app checks on every page if my user is logged in or not. I have 2 cookies set in my signin process - which works great. now I want to set a global var if a user is logged in or not I do that in the application controller
> 
> class ApplicationController \< ActionController::Base
> 
> &nbsp;&nbsp;&nbsp;&nbsp;$login\_user\_id = cookies[:user\_id] &nbsp;&nbsp;&nbsp;&nbsp;$login\_user\_sid = cookies[:user\_sid] &nbsp;&nbsp;&nbsp;&nbsp;if $login\_user\_id != "" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$login\_ok = true &nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$login\_ok = false &nbsp;&nbsp;&nbsp;&nbsp;end
> 
> end
> 
> but this will not work - he cant access the cookies
> 
> error \>\> undefined local variable or method `cookies' for ApplicationController:Class
> 
> what I am doing wrong?
> 
> thanks for any help in my path to escape the MS ASP world 😉andreas

Hey

You could write an action that just checks those cookies...

class ApplicationController \< ActionController::Base

&nbsp;&nbsp;def login\_ok? &nbsp;&nbsp;&nbsp;&nbsp;!cookies[:user\_id].blank? &nbsp;&nbsp;end

end

Then in some other action in say, your store\_controller? use

def show\_store &nbsp;&nbsp;if login\_ok? &nbsp;&nbsp;&nbsp;&nbsp;render :layout =\> 'store' &nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;flash[:warning] = 'not allowed there!' &nbsp;&nbsp;&nbsp;&nbsp;redirect\_to :action =\> 'not\_allowed' &nbsp;&nbsp;end end

Or something similar, I'm not very big on cookies, but this seems like it should work :]

Cheery-o Gustav Paul gustav@rails.co.za
