redirect_to
- 1.0.0 (0)
- 1.1.6 (4)
- 1.2.6 (0)
- 2.0.3 (8)
- 2.1.0 (0)
- 2.2.1 (1)
- 2.3.8 (10)
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7
- 5.2.3
- 6.0.0
- 6.1.3.1
- 6.1.7.7
- 7.0.0
- 7.1.3.2
- 7.1.3.4
- What's this?
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
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.
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")
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
-
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
If needed, you can also specify a controller.
redirect_to :controller => 'post', :action => 'index'
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
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'
redirect_to :root
You can redirect to your main page using
redirect_to :root
Make sure to configure to root route first: