method

redirect_to

redirect_to(options = {}, response_status = {})
protected

Redirects the browser to the target specified in options. This parameter can take one of three forms:

  • Hash - The URL will be generated by calling url_for with the options.
  • Record - The URL will be generated by calling url_for with the options, which will reference a named URL for that record.
  • String starting with protocol:// (like http://) - Is passed straight through as the target for redirection.
  • String not containing a protocol - The current protocol and host is prepended to the string.
  • :back - Back to the page that issued the request. Useful for forms that are triggered from multiple places. Short-hand for redirect_to(request.env["HTTP_REFERER"])

Examples:

  redirect_to :action => "show", :id => 5
  redirect_to post
  redirect_to "http://www.rubyonrails.org"
  redirect_to "/images/screenshot.jpg"
  redirect_to articles_url
  redirect_to :back

The redirection happens as a "302 Moved" header unless otherwise specified.

Examples:

  redirect_to post_url(@post), :status => :found
  redirect_to :action=>'atom', :status => :moved_permanently
  redirect_to post_url(@post), :status => 301
  redirect_to :action=>'atom', :status => 302

It is also possible to assign a flash message as part of the redirection. There are two special accessors for commonly used the flash names alert and notice as well as a general purpose flash bucket.

Examples:

  redirect_to post_url(@post), :alert => "Watch it, mister!"
  redirect_to post_url(@post), :status=> :found, :notice => "Pay attention to the road"
  redirect_to post_url(@post), :status => 301, :flash => { :updated_post_id => @post.id }
  redirect_to { :action=>'atom' }, :alert => "Something serious happened"

When using redirect_to :back, if there is no referrer, RedirectBackError will be raised. You may specify some fallback behavior for this case by rescuing RedirectBackError.

6Notes

flash messages

daniely · Mar 9, 20123 thanks

In rails 3.1 the following does not work for me

redirect_to { :action=>'atom' }, :alert => "Something serious happened" 

Instead, you need to use the following syntax (wrap with parens)

redirect_to({ :action=>'atom' }, :alert => "Something serious happened")

Status Codes

nhance · Jan 7, 20101 thank

Full detail: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes

  • 100 - Continue
  • 101 - Switching Protocols
  • 200 - OK
  • 201 - Created
  • 202 - Accepted
  • 203 - Non-Authoritative Information
  • 204 - No Content
  • 205 - Reset Content
  • 206 - Partial Content
  • 300 - Multiple Choices
  • 301 - Moved Permanently
  • 302 - Found
  • 303 - See Other
  • 304 - Not Modified
  • 305 - Use Proxy
  • 306 - No Longer Used
  • 307 - Temporary Redirect
  • 400 - Bad Request
  • 401 - Not Authorised
  • 402 - Payment Required
  • 403 - Forbidden
  • 404 - Not Found
  • 405 - Method Not Allowed
  • 406 - Not Acceptable
  • 407 - Proxy Authentication Required
  • 408 - Request Timeout
  • 409 - Conflict
  • 410 - Gone
  • 411 - Length Required
  • 412 - Precondition Failed
  • 413 - Request Entity Too Large
  • 414 - Request URI Too Long
  • 415 - Unsupported Media Type
  • 416 - Requested Range Not Satisfiable
  • 417 - Expectation Failed
  • 500 - Internal Server Error
  • 501 - Not Implemented
  • 502 - Bad Gateway
  • 503 - Service Unavailable
  • 504 - Gateway Timeout
  • 505 - HTTP Version Not Supported

Specify controller

batasrki · Mar 3, 2009

If needed, you can also specify a controller.

redirect_to :controller => 'post', :action => 'index'

This code prevent's frome redirect_to(:back) looping

pkondzior · Oct 1, 2009

=== Prevent redirect_to(:back) looop

begin # loop check if session[:last_back] != request.env['HTTP_REFERER'] redirect_to(:back) session[:last_back] = request.env['HTTP_REFERER'] else # raise on error raise ActionController::RedirectBackError end rescue ActionController::RedirectBackError # fallback on loop or other :back error redirect_to(:action => :index) end

You can specify the format as well

insane-dreamer · Nov 30, 2009

You can also specify the format (in case you need to redirect a request coming in one format to another format):

redirect_to :action => 'show', :format => 'html'

redirect_to :root

gdelfino · Jul 16, 2010

You can redirect to your main page using

redirect_to :root

Make sure to configure to root route first:

http://guides.rubyonrails.org/routing.html#using-maproot