Using flash to display notices in the browser - Ramaze/ramaze GitHub Wiki
You can think of flash as a placeholder hash that will store volatile data for the next request. This data is available in views and layouts, and is used to display informative messages in the browser (e.g. something went wrong, something succeeded).
For instance, let's say you need to display an alert box in the page for the following situations :
- errors (oops, something went wrong)
- informations (user notification about something normal)
- success (form has been handled properly)
You can decide on symbols (:error
, :info
, :success
) and check in
your layout if flash has one of those keys set. The example below does
just this, and also use the key to set the CSS class for the alert box :
<?r [:success, :error, :info].each do |type| ?>
<?r if flash[type] ?>
<div class="alert alert-block alert-#{type} fade in">
<a class="close" data-dismiss="alert" href="#">×</a>
<h4 class="alert-heading">#{type.capitalize}</h4>
<p>#{flash[type]}</p>
</div>
<?r end ?>
<?r end ?>
Then, if you set :
flash[:error] = 'Invalid username'
in your controller you will display an alert box containing 'Invalid username' (this example uses twitter bootstrap which brings CSS facilities to display those).