Redirecting to a Different Location

Problem

You want to redirect your user to another of your applications 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.

Heres 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 => ureaucracy, :action => 
eservation_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, youll 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 need to create view templates for all of these actions, because the body of an HTTP redirect isn displayed by the web browser.

Discussion

The redirect_to method uses smart defaults. If you give it a hash that doesn 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 do a redirect: it tells Rails to do a redirect once the action method has finished running.

Heres 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 . The only way to stop an action method from running all the way to the end is to call return.[2] What you really want to do is this:

[2] You could throw an exception, but then your redirect wouldn happen: the user would see an exception screen instead.

	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. Its very rare that youll 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


Strings

Numbers

Date and Time

Arrays

Hashes

Files and Directories

Code Blocks and Iteration

Objects and Classes8

Modules and Namespaces

Reflection and Metaprogramming

XML and HTML

Graphics and Other File Formats

Databases and Persistence

Internet Services

Web Development Ruby on Rails

Web Services and Distributed Programming

Testing, Debugging, Optimizing, and Documenting

Packaging and Distributing Software

Automating Tasks with Rake

Multitasking and Multithreading

User Interface

Extending Ruby with Other Languages

System Administration



Ruby Cookbook
Ruby Cookbook (Cookbooks (OReilly))
ISBN: 0596523696
EAN: 2147483647
Year: N/A
Pages: 399

Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net