Home arrow Ruby-on-Rails arrow Page 3 - 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.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


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 11 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials