Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Page 2 - Flash and the Rails Action Controller
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
RUBY-ON-RAILS

Flash and the Rails Action Controller
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-01-31

    Table of Contents:
  • Flash and the Rails Action Controller
  • 4.5 Displaying Alert Messages with Flash
  • 4.6 Extending the Life of a Flash Message
  • 4.7 Following Actions with Redirects

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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

    More Ruby-on-Rails Articles
    More By O'Reilly Media


       · This article is an excerpt from the "Rails Cookbook," published by O'Reilly. We hope...
     

    Buy this book now. This article is excerpted from chapter four of the Rails Cookbook, written by Rob Orsini (O'Reilly, 2007; ISBN: 0596527314). Check it out today at your favorite bookstore. Buy this book now.

    RUBY-ON-RAILS ARTICLES

    - Iterating and Incrementing Strings in Ruby
    - Comparing and Manipulating Strings in Ruby
    - Strings in Ruby
    - Ruby On Rails: Making Your First Dynamic Site
    - Ruby on Rails: Beginning Rails
    - Ruby: Modules, Mixins, Fixins, and Rails
    - Controlling Information Access with the Rail...
    - URLs, Filters and the Rails Action Controller
    - Flash and the Rails Action Controller
    - Rails Action Controller
    - Dropping and Sorting with AJAX and script.ac...
    - Drag and Drop with script.aculo.us and Rails
    - Introducing script.aculo.us
    - Ruby Classes and Objects
    - Ruby Loops







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT