Question about flash

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?

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

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?

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.

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

szymek wrote:

In my case redirect_to DOES call the action. I’ve just tried it :slight_smile:

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

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.

In my case redirect_to DOES call the action. I’ve just tried it :slight_smile: