# Redirect\_to kills my session variable

**URL:** <https://www.ruby-forum.com/t/redirect-to-kills-my-session-variable/194914>\
**Category:** Rails\
**Created:** [September 13, 2010, 5:48pm UTC](https://www.ruby-forum.com/t/redirect-to-kills-my-session-variable/194914 "2010-09-13T17:48:07Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [September 13, 2010, 5:48pm UTC](https://www.ruby-forum.com/t/redirect-to-kills-my-session-variable/194914/1 "2010-09-13T17:48:07Z")

</div>

Hi,

I’m trying to set a session variable in one action of  
application\_controller.rb, which is then accessed in a second action of  
the same controller following a redirect.

Here’s the code.

def cookie\_test  
if cookies[“cookie\_test”].blank?  
render :template =\> “shared/cookies\_required”  
else  
#redirect back to where the user came from  
end  
end

protected

def cookies\_required  
return true unless cookies[“cookie\_test”].blank?  
session[:return\_to] = request.request\_uri  
cookies[“cookie\_test”] = Time.now  
redirect\_to :action =\> “cookie\_test” and return false  
end

The method ‘cookies\_required’ is called first, which sets the session  
variable ‘session[:return\_to]’.

I would like to be able to access this variable in the method  
‘cookie\_test’ following the redirect.

However, when I try to do so, it shows up as nil.

I tried replacing the session variable with a global variable and that  
worked fine.  
I also tried sending the ‘request.request\_uri’ variable as a parameter  
to the second action and that worked too.

But why is my session variable dying following the redirect??

Thanks in advance
