Flowdock
method

redirect_to

Importance_5
Ruby on Rails latest stable (v6.1.7.7) - 6 notes - Class: ActionController::Base

Method deprecated or moved

This method is deprecated or moved on the latest stable version. The last existing version (v2.3.8) is shown here.

These similar methods exist in v6.1.7.7:

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.

Show source
Register or log in to add new notes.
March 10, 2012
3 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