Home arrow Ruby-on-Rails arrow Page 2 - Flash and the Rails Action Controller
RUBY-ON-RAILS

Flash and the Rails Action Controller


In this second part of a four-part series on the Rails Action Controller, you'll learn how to display alert messages with Flash, how to extend the life of a Flash message, and more. This article is excerpted from chapter four of the Rails Cookbook, written by Rob Orsini (O'Reilly, 2007; ISBN: 0596527314). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 5 stars5 stars5 stars5 stars5 stars / 2
January 31, 2008
TABLE OF CONTENTS:
  1. · Flash and the Rails Action Controller
  2. · 4.5 Displaying Alert Messages with Flash
  3. · 4.6 Extending the Life of a Flash Message
  4. · 4.7 Following Actions with Redirects

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Flash and the Rails Action Controller - 4.5 Displaying Alert Messages with Flash
(Page 2 of 4 )

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 style="color: red;"><%= 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 style="color: green;"><%= 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


blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form
- Ruby on Rails Templates and Layouts
- Action Pack Controller Creation
- Writing an Action Pack Controller

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials