The flash is a special part of the session which is cleared with each request. This means that values stored there will only be available in the next request, which is useful for passing error messages etc.
It is accessed in much the same way as the session, as a hash (it's a FlashHash instance).
Let's use the act of logging out as an example. The controller can send a message which will be displayed to the user on the next request:
Note that it is also possible to assign a flash message as part of the redirection.
You can assign
:alert or the general purpose
destroy action redirects to the application's
root_url, where the message will be displayed. Note that it's entirely up to the next action to decide what, if anything, it will do with what the previous action put in the flash.
It's conventional to display any error alerts or notices from the flash in the application's layout:
This way, if an action sets a notice or an alert message, the layout will display it automatically.
You can pass anything that the session can store; you're not limited to notices and alerts:
If you want a flash value to be carried over to another request, use the
By default, adding values to the flash will make them available to the next request, but sometimes you may want to access those values in the same request.
For example, if the
create action fails to save a resource and you render the
new template directly,
that's not going to result in a new request, but you may still want to display a message using the flash.
To do this, you can use
flash.now in the same way you use the normal