# Question about flash

**URL:** <https://www.ruby-forum.com/t/question-about-flash/55878>\
**Category:** Rails\
**Created:** [March 24, 2006, 2:38pm UTC](https://www.ruby-forum.com/t/question-about-flash/55878 "2006-03-24T14:38:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 24, 2006, 2:38pm UTC](https://www.ruby-forum.com/t/question-about-flash/55878/1 "2006-03-24T14:38:03Z")

</div>

Hi!

I’ve got a search form, which calls search action. If the result set is  
empty, i set flash to display a proper message. At the end search action  
calls “render :action =\> list”.

If the search action returns empty result set and the message is  
displayed, but later user changes searching conditions, calls search  
again and result set is no longer empty, the message is still displayed  
along with the new result set. If i reload the page, the message  
disappears.

Do i have to reset the flash somewhere?

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 24, 2006, 5:08pm UTC](https://www.ruby-forum.com/t/question-about-flash/55878/2 "2006-03-24T17:08:41Z")

</div>

The idea of the flash is that it holds on to something until the next  
request. So, when  
you “render” your results, you are generating the response within the  
same post request…  
the flash will not be cleared because it’s the same request. That’s why  
it goes away when  
you hit “reload”… cuz it’s a new request.

The preferred way to fix this is to “redirect” instead of “render”…  
then you are on the  
new request when you get your search results and clicking reload won’t  
do the form  
submission again (considered bad usability).

b

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 24, 2006, 5:36pm UTC](https://www.ruby-forum.com/t/question-about-flash/55878/3 "2006-03-24T17:36:18Z")

</div>

Ok i think i understand it… but it doesn’t work.

I have these 2 actions: list and search.

List action paginates objects in the table with a single condition  
(there are links in the menu to it with different parameters) or with no  
conditions (returns all objects).

Search does similiar thing but with few conditions (which are received  
from the form).

I use only one view for both of them: list.rhtml. If i change “render  
:action =\> ‘list’” at the end of the search action to “redirect\_to  
:action =\> ‘list’”, it just calls list action and i get all objects, so  
the data from the search action is lost.

As I understand it, redirect\_to creates a new request and that’s the  
reason why there’s no data from search action. If it’s true, then how to  
do it correctly?

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 24, 2006, 5:44pm UTC](https://www.ruby-forum.com/t/question-about-flash/55878/4 "2006-03-24T17:44:47Z")

</div>

While writing the previous post i realized i could join these 2 actions  
and check if received parameters were sent by get or post.

So i don’t have any render or redirect\_to now, because action name and  
view name are the same.

But now it’s even worse. If i use search form and it doesn’t return any  
results, and later i click on the link in the menu to display all  
objects, the message is still there.

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 24, 2006, 5:59pm UTC](https://www.ruby-forum.com/t/question-about-flash/55878/5 "2006-03-24T17:59:27Z")

</div>

szymek wrote:

> I use only one view for both of them: list.rhtml. If i change “render  
> :action =\> ‘list’” at the end of the search action to “redirect\_to  
> :action =\> ‘list’”, it just calls list action and i get all objects, so  
> the data from the search action is lost.
> 
> As I understand it, redirect\_to creates a new request and that’s the  
> reason why there’s no data from search action. If it’s true, then how to  
> do it correctly?

Put your search results object (array?) in the flash and then change the  
template code to  
get it from the flash. Altough… someone might have other ideas here.

One thing I did just learn the other day is that “redirect =\> ‘foo’” or  
“render =\> ‘foo’”  
does NOT call the action… it calls the view. Seems counterintuitive I  
know…

b

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 25, 2006, 1:12am UTC](https://www.ruby-forum.com/t/question-about-flash/55878/6 "2006-03-25T01:12:09Z")

</div>

szymek wrote:

> In my case redirect\_to DOES call the action. I’ve just tried it 🙂

Oh duh, yeah… of course… _redirect_ will call the action cuz it’s a  
new request, render just goes to the view…  
that actually makes sense to me now.

b

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 25, 2006, 11:12am UTC](https://www.ruby-forum.com/t/question-about-flash/55878/7 "2006-03-25T11:12:33Z")

</div>

I decided to reset flash at the beginning of the combined list/search  
action. I just create empty flash and in my view check if it’s empty or  
not.  
This way despite the fact that there’s no new request, the flash from  
previous call doesn’t exist anymore.  
Not sure if it’s ‘the right way’ to do it (or if there’s any), but it  
works.

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 24, 2006, 6:12pm UTC](https://www.ruby-forum.com/t/question-about-flash/55878/8 "2006-03-24T18:12:07Z")

</div>

In my case redirect\_to DOES call the action. I’ve just tried it 🙂
