Home arrow Ruby-on-Rails arrow Page 4 - 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.7 Following Actions with Redirects
(Page 4 of 4 )

 

Problem

Submitting a form in your application calls an action that updates your model. You want this action to redirect to a second action that will handle rendering. This way, when the response is sent, the user will see a new URL; refreshing the page will not re-initiate the first action.

Solution

Call redirect_to, as in the following controller’s new action:

app/controllers/password_controller.rb:

  class AccountController
< ApplicationController

    def list
   
end

    def new
     
@account = Account.new(params[:account])
     
if @account.save
       
flash[:notice] = 'Account was successfully created.'
        redirect_to :action => 'list'
      end
    end
  end

Discussion

The solution defines a new method that attempts to create a new account. If a newly created account is saved successfully, the new method stores a flash notice and calls redirect_to to redirect to the controller’s list action.

redirect_to takes an options hash as an argument. Internally, this hash is passed to url_for to create a URL. If it’s passed a string that begins with protocol information (e.g., http://), it uses the string as the entire relocation target. Otherwise, it interprets the string as a relative URI. Finally, redirect_to can be passed the symbol :back, which tells the browser to redirect to the referring URL or the contents of request.env["HTTP_REFERER"].

Redirection works by sending the browser an HTTP/1.1 302 Found status code, telling the browser that “the requested resource resides temporarily under a different URI,” or simply that it should redirect to the URI supplied in this response. This prevents users from creating duplicate accounts with their refresh button, because refreshing only reloads the list template.

A common question on the rubyonrails mailing list is when to use render, instead of redirect_to. As this solution demonstrates, if you don’t want a refresh to re-initiate an action that makes changes to your model, use redirect_to. If you want a search form URL, such as /books/search, to remain the same, even when results of the search are displayed by a new action, use render. (When running in development mode, renders are faster than redirects because they don’t reload the environment.)

action that makes changes to your model, use redirect_to. If you want a search form URL, such as , to remain the same, even when results of the search are displayed by a new action, use render. (When running in development mode, renders are faster than redirects because they don’t reload the environment.)

See Also

Please check back next week for the continuation of this article.


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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