# Redirecting if cookie doesn't exist

**URL:** <https://www.ruby-forum.com/t/redirecting-if-cookie-doesnt-exist/91487>\
**Category:** Rails\
**Created:** [April 9, 2007, 6:39pm UTC](https://www.ruby-forum.com/t/redirecting-if-cookie-doesnt-exist/91487 "2007-04-09T18:39:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![azraelnewtype](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@azraelnewtype](https://www.ruby-forum.com/u/azraelnewtype)\
**Post date:** [April 9, 2007, 6:39pm UTC](https://www.ruby-forum.com/t/redirecting-if-cookie-doesnt-exist/91487/1 "2007-04-09T18:39:15Z")

</div>

Hi all,

I cannot get this to work at all … it never does the redirect.

I have two authentication cookies set … Rails reads thier values fine  
if  
they exist

If they don’t exist, I want to redirect the user to the login page –  
which  
is not a rails controller/model …(single sign on location)

I figured I’d add the code to the application\_helper … so that I could  
reference it in all my views.

module ApplicationHelper

def ifLoggedIn()  
useridcookie=cookies[:UserID]  
usersessioncookie=cookies[:usersession]  
if (!useridcookie)  
redirect\_to “[http://loginpage.html](http://loginpage.html)”  
end  
if (!usersessioncookie)  
redirect\_to [http://loginpage.html](http://loginpage.html)  
end  
end  
end

* * *

in my view (list.rhtml), I call \<% ifLoggedIn() %\>  
right at the top

Any help much appreciated!

–  
Chris

---

<div class="post-metadata">

**Author:** ![azraelnewtype](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@azraelnewtype](https://www.ruby-forum.com/u/azraelnewtype)\
**Post date:** [April 9, 2007, 8:40pm UTC](https://www.ruby-forum.com/t/redirecting-if-cookie-doesnt-exist/91487/2 "2007-04-09T20:40:31Z")

</div>

Well, in the example you posted, you don’t have quotes around the second  
“[http://loginpage.html/](http://loginpage.html/)”… though maybe that’s just a error in copying.

Other than that, maybe try some logging in there to see what’s  
happening…

b

PS: Oh, one more thing… I think most people usually do their logged in  
check in the ApplicationController (application.rb) and hook it up as a  
before\_filter for all their actions. That way you won’t have to put  
ifLoggedIn in every single view.
