# Redirects and keeping flash

**URL:** <https://www.ruby-forum.com/t/redirects-and-keeping-flash/97825>\
**Category:** Rails\
**Created:** [July 10, 2006, 8:08pm UTC](https://www.ruby-forum.com/t/redirects-and-keeping-flash/97825 "2006-07-10T20:08:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [July 10, 2006, 8:08pm UTC](https://www.ruby-forum.com/t/redirects-and-keeping-flash/97825/1 "2006-07-10T20:08:40Z")

</div>

My code that acts if a session has timed out looks like this:

reset\_session  
session[:expires\_at] = nil

# TODO - This flash is never shown, because we redirect maybe?

flash[:error] = ‘Your session has timed out, please login to  
continue.’

# An attempt to make flash stick around, not working

flash.keep  
redirect\_to :controller =\> ‘security’, :action =\> ‘login’ and return  
false

The redirect works fine, but the flash does not. Before I discovered  
reset\_session I was clearing the keys I didn’t need (e.g.  
session[:user\_id]) one by one. I believe this showed the flash, but it  
doesn’t seem like the best idea if I want to add more to the  
authenticated users session (i.e. I may forget to clear out a variable).

Is there any way to fix the above without specifying each and every  
variable that needs to be cleared out?

Thanks.

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [July 14, 2006, 3:58pm UTC](https://www.ruby-forum.com/t/redirects-and-keeping-flash/97825/2 "2006-07-14T15:58:13Z")

</div>

David wrote:

> My code that acts if a session has timed out looks like this:
> 
> reset\_session  
> session[:expires\_at] = nil
> 
> # TODO - This flash is never shown, because we redirect maybe?
> 
> flash[:error] = ‘Your session has timed out, please login to  
> continue.’
> 
> # An attempt to make flash stick around, not working
> 
> flash.keep  
> redirect\_to :controller =\> ‘security’, :action =\> ‘login’ and return  
> false
> 
> The redirect works fine, but the flash does not. Before I discovered  
> reset\_session I was clearing the keys I didn’t need (e.g.  
> session[:user\_id]) one by one. I believe this showed the flash, but it  
> doesn’t seem like the best idea if I want to add more to the  
> authenticated users session (i.e. I may forget to clear out a variable).

I have not been able to fix this, but I have made a rather hackish  
solution which seems to work in my application.

I believe the actual underlying problem could be caused by this ticket:

[http://dev.rubyonrails.org/ticket/5584](http://dev.rubyonrails.org/ticket/5584)

To get round this, I have created a method called “clear\_session”. All  
this does is set session[:deleted] = true. After this, I immediately  
redirect to the login form. The first thing the login controller does  
is to run:

reset\_session if session[:deleted]

This isn’t perfect, but it works in my application and most importantly,  
allows the flash to be shown on the login page. When the ticket above  
is closed, I’ll just replace my clear\_session throughout the code with  
reset\_session.

If anybody sees any glaring bugs here, please inform me! 🙂
