dstarh
dstarh

Reputation: 5086

Play framework flash scope does not clear on ajax

If I have a controller method that sets flash.success("some.i18n.key"); and I render a page that is loaded via ajax that item does not get removed from flash. Even though I've rendered the content to the screen (html loaded into a div in the success handler of my ajax post) the next page I visit still has the success message in flash. Pages that work with a normal form post,non ajax) this issue does not happen. Any idea whats going on?

Further investigation seems like this might be some sort of race condition. When I do a normal post and the FLASH cookie is returned it expires immediately and on the next request it is not sent back to the server. In the case of the AJAX post and then a subsequent request the cookie IS sent back to the server.

Upvotes: 1

Views: 1441

Answers (2)

Patrik Beck
Patrik Beck

Reputation: 2505

Since play 2 they changed the flashing a bit, instead of 2 maps (incoming, outgoing) there is just one.

What I end up doing is calling:

@flash.clear()

Just after the flash messages are rendered (in the view). This way, you are sure they are rendered just once, regardless of weather you use direct render, or redirect.

Upvotes: 1

Seb Cesbron
Seb Cesbron

Reputation: 3833

flash values are kept for one redirect. If you call render in your controller at the end of your method, you do not issue a redirect, so values will be available for the next request. To avoid this you have the choice :

  1. use renderArgs in your method to pass your value to the view
  2. at the end of your method, do not call render but call another method of the controller, thus you will issue a redirect instead of a direct render.

Upvotes: 2

Related Questions