Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Page 3 - 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  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
IBM developerWorks
 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Flash and the Rails Action Controller - 4.6 Extending the Life of a Flash Message


    (Page 3 of 4 )

     

    Problem

    You’ve created a flash message and are displaying it to the user. You’d like to extend the life of that message for one more request than would normally exist.

    Solution

    You can call the keep method of the Flash class on a specific entry, or the entire contents of the flash hash. This technique is useful for redisplaying flash messages in subsequent requests without explicitly recreating them.

    To demonstrate this, create the following RentalController:

    app/controllers/rental_controller.rb:

      class RentalController
    < ApplicationController

        def step_one
         
    flash.now[:reminder] = 'There is a $20 fee for late
    payments.'
         
    flash.keep(:reminder)
        end
        def step_two
        end

        def step_three
        end
      end
     

    And create the following three views:

    app/views/rental/step_one.rhtml:

      <h1>Step one!</h1>
      <% if flash[:reminder] %>
        <p style="color: green;"><%= flash[:reminder] %></p>
      <% end %>
      <a href="step_two">step_two</a>

    app/views/rental/step_two.rhtml:

      <h1>Step two!</h1>
      <% if flash[:reminder] %>
        <p style="color: green;"><%= flash[:reminder] %></p>
      <% end %>
      <a href="step_three">step_tree</a>

    app/views/rental/step_three.rhtml:

      <h1>Step three!</h1>
      <% if flash[:reminder] %>
        <p style="color: green;"><%= flash[:reminder] %></p>
      <% end %>
      <a href="step_one">step_one</a>

    Discussion

    As you can see in the solution, the controller creates a flash message only in the action called step_one.

    From a browser, in the first step you see the reminder on the screen. When you click on the link at the bottom of the page, you call step_two. Now the flash message is shown a second time.

    Step three is like step two, but we didn’t call the flash.keep in this method, and the message doesn’t reappear. The keep method holds the reminder for only one request.

    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

    - 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
    - Ruby Conditionals
    - Ruby Operators and Arrays
    - Ruby for the Newbie







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway