Displaying Alert Messages with Flash

Problem

You've created an informative message while processing the current request. You want this message to be available for display during the next request. Additionally, the message should cease to be available following the next request.

Solution

You have a form that requests the user to enter a password that meets a certain criteria.

views/password/form.rhtml:

<h2>Please choose a good password:</h2>
<p ><%= flash[:notice] %></p>
<% form_tag(:action => 'check') do %>
 <input type="text" name="pass">
 <input type="submit">
 <p>(8 character minimum, at least 2 digits)</p>
<% end %>

The form submits to the Check controller, which strips the password candidate of all whitespace, and then a couple of regular expressions test that the password meets the criteria. The tests are broken up to provide more specific error message notifications.

If both matches succeed, the request is redirected to the success action and passed along to :pass for display. If either check fails, the request redirects back to the form action.

app/controllers/password_controller.rb:

class PasswordController < ApplicationController
 def form
 end
 def check
 password = params['pass'].strip.gsub(/ /,'')
 if password =~ /\w{8}/
 flash[:notice] = "Your password is long enough" 
 if password =~ /\d+.*\d+/
 flash[:notice] += " and contains enough digits." 
 redirect_to :action => 'success', :pass => password
 return
 else 
 flash[:notice] = "Sorry, not enough digits." 
 end 
 else 
 flash[:notice] = "Sorry, not long enough." 
 end
 redirect_to :action => 'form'
 end
 def success
 @pass = params['pass']
 end end

Upon success, the user is redirected to success.rthml , and his password is displayed (without any whitespace it may have contained):

views/password/success.rthml:

<h2>Success!</h2>
<% if flash[:notice] %>
 <p ><%= flash[:notice] %></p>
<% end %>

Discussion

Building a usable web application hinges on keeping the user informed about what's going on, and why things happen. Communicative alert messages are an integral part of most good applications. Displaying such messages is so common that Rails has a facility for doing so called the flash.

Internally, the flash is just a hash stored in the session object. It has the special quality of getting cleared out after the very next request (though you can alter this behavior with the flash.keep method).

Redirecting with redirect_to is often used to display a new URL in the location bar of the browser, somewhat hiding the inner workings of an application. Because messages stored in the flash are just stored in the session object, they are available across such redirects, unlike instance variables. And since they last only for one more request, hitting the refresh button makes the message disappear. From the user's perspective, this is usually the ideal behavior.

If you find yourself tempted to use the flash to store more than just user notification messages (e.g., object IDs), make sure to consider whether using the standard session object would work as well or better.

See Also