Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Options for Web Applications with Ruby on ...
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

Options for Web Applications with Ruby on Rails
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2007-03-29

    Table of Contents:
  • Options for Web Applications with Ruby on Rails
  • 15.5 Displaying Templates with Render
  • 15.6 Integrating a Database with Your Rails Application
  • 15.7 Understanding Pluralization Rules

  • 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


    Options for Web Applications with Ruby on Rails


    (Page 1 of 4 )

    In this second part of a six-part series on web development and Ruby on Rails, you'll learn how to integrate a database with your RoR application and more. This article is excerpted from chapter 15 of the Ruby Cookbook, written by Lucas Carlson and Leonard Richardson (O'Reilly, 2006; ISBN: 0596523696). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

    15.4 Redirecting to a Different Location

    Problem

    You want to redirect your user to another of your application's actions, or to an external URL.

    Solution

    The class ActionController::Base (superclass of ApplicationController) defines a method called redirect_to, which performs an HTTP redirect. To redirect to another site, you can pass it a URL as a string. To redirect to a different action in your application, pass it a hash that specifies the controller, action, and ID.

    Here's a BureaucracyController that shuffles incoming requests to and fro between various actions, finally sending the client to an external site:

      class BureaucracyController < ApplicationController
        def index
          redirect_to :controller => 'bureaucracy', :action => 'reservation_window'
        end

        def reservation_window
          redirect_to :action => 'claim_your_form', :id => 123
        end

        def claim_your_form
          redirect_to :action => 'fill_out_your_form', :id => params[:id]
        end

        def fill_out_your_form
          redirect_to :action => 'form_processing'
        end

        def form_processing
          redirect_to http://www.dmv.org/
        end
      end

    If you run the Rails server and hit http://localhost:3000/bureaucracy/ in your browser, you'll end up at http://www.dmv.org/. The Rails server log will show the chain of HTTP requests you made to get there:

      "GET /bureaucracy HTTP/1.1" 302
      "GET /bureaucracy/reservation_window HTTP/1.1" 302
      "GET /bureaucracy/claim_your_form/123 HTTP/1.1" 302
      "GET /bureaucracy/fill_out_your_form/123 HTTP/1.1" 302
      "GET /bureaucracy/form_processing HTTP/1.1" 302

    You don't need to create view templates for all of these actions, because the body of an HTTP redirect isn't displayed by the web browser.

    Discussion

    The redirect_to method uses smart defaults. If you give it a hash that doesn't specify a controller, it assumes you want to move to another action in the same controller. If you leave out the action, it assumes you are talking about the index action.

    From the simple redirects given in the Solution, you might think that calling redirect_to actually stops the action method in place and does an immediate HTTP redirect. This is not true. The action method continues to run until it ends or you call return. The redirect_to method doesn't do a redirect: it tells Rails to do a redirect once the action method has finished running.

    Here's an illustration of the problem. You might think that the call to redirect_to below prevents the method do_something_dangerous from being called.

      class DangerController < ApplicationController
       
    def index
         
    redirect_to (:action => 'safety') unless params[:i_like_danger]
          do_something_dangerous
       
    end

        # .. .
      end

    But it doesn't. The only way to stop an action method from running all the way to the end is to call return.* What you really want to do is this:

      class DangerController < ApplicationController
       
    def index
         
    redirect_to (:action => 'safety') and return unless params[:i_like_danger]
          do_something_dangerous
       
    end
      end

    Notice the and return at the end of redirect_to. It's very rare that you'll want to execute code after telling Rails to redirect the user to another page. To avoid problems, make a habit of adding and return at the end of calls to redirect_to or render.

    See Also

    • The generated RDoc for the methods ApplicationController::Base#redirect_to and ApplicationController::Base#url_for

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


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

    Buy this book now. This article is excerpted from chapter 15 of the Ruby Cookbook, written by Lucas Carlson and Leonard Richardson (O'Reilly, 2006; ISBN: 0596523696). 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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek