Flowdock
method

redirect_to

Importance_4
redirect_to(options = {}, *parameters_for_method_reference) 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.
  • 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 "http://www.rubyonrails.org"
  redirect_to "/images/screenshot.jpg"
  redirect_to :back

The redirection happens as a "302 Moved" header.

Show source
Register or log in to add new notes.
March 10, 2012
2 thanks

flash messages

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")
January 8, 2010
1 thank

Status Codes

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

March 3, 2009
0 thanks

Specify controller

If needed, you can also specify a controller.

redirect_to :controller => 'post', :action => 'index'
October 1, 2009
0 thanks

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

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

November 30, 2009
0 thanks

You can specify the format as well

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'
July 17, 2010
0 thanks

redirect_to :root

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