Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8297 (closed enhancement: fixed)

Opened 2 years ago

Last modified 1 year ago

[PATCH] Add :status support to redirect_to

Reported by: codahale Assigned to: core
Priority: normal Milestone: 2.x
Component: ActionPack Version: edge
Severity: normal Keywords:
Cc: tom

Description

ActionController::Base#redirect_to currently only renders temporary 302 Found responses. In order to render a permanent redirect (301 Moved Permanently), one must manually set the status on the response:

def perm_redirect
  response.headers["Status"] = "301 Moved Permanently"
  redirect_to :action => "the_real_deal"
end

For such a simple operation, this is super ugly.

This patch allows for the use of the common :status option:

redirect_to book_url(@book), :status => 301
redirect_to "http://www.example.com", :status => :temporary_redirect
redirect_to({ :action => "the_real_deal" }, :status => :permanently_moved)
redirect_to :back, :status => 301 # For consistency's sake; I can't imagine someone using this.

When not specified, redirect_to falls back on the default of using 302 Found.

I think it's important for the framework to have this support baked in because of the importance of 301 redirects in keeping a web site's Google Rank constant. 302 redirects do not transfer the Google Fu, 301 redirects do.

Judging by the sheer number of blog entries and mailing list hits, I think there's a pretty strong demand for this functionality <http://www.google.com/search?q=rails%20redirect_to%20301>.

This patch includes full unit tests, and passes existing unit tests.

Attachments

redirect_status_support.diff (5.2 kB) - added by codahale on 05/08/07 10:04:31.
redirect_to support for the :status option
redirect_with_status_and_back_default.diff (5.8 kB) - added by codahale on 05/13/07 18:24:12.
Better patch, now including redirect back with default.
redirect_to_with_status_codes.diff (5.9 kB) - added by chasgrundy on 05/16/07 20:08:36.
Leaner, cleaner patch for adding status codes

Change History

05/08/07 10:04:31 changed by codahale

  • attachment redirect_status_support.diff added.

redirect_to support for the :status option

05/08/07 15:13:44 changed by bgreenlee

+1

05/13/07 17:53:01 changed by codahale

Crap, broken by r6729. Will fix.

05/13/07 18:24:12 changed by codahale

  • attachment redirect_with_status_and_back_default.diff added.

Better patch, now including redirect back with default.

05/13/07 18:26:51 changed by codahale

Ok, now compatible with r6729.

Also includes the ability to specify a default URL for redirect_to :back.

redirect_to :back, :default => { :controller => "home", :action => "index" }

05/16/07 20:08:36 changed by chasgrundy

  • attachment redirect_to_with_status_codes.diff added.

Leaner, cleaner patch for adding status codes

05/16/07 20:10:56 changed by chasgrundy

I've submitted a different patch, based on the same concept. Rather than try and include back_with_default or anything "new," this patch simply lets you add a :status => 301 (or whatever status code) to the redirect_to. It uses the built-in status codes rather than hard-coding alternatives. I also expanded tests a bit. I thought it was cleaner and a little more robust.

05/24/07 15:55:18 changed by tom

  • cc set to tom.

09/23/07 16:51:25 changed by therealadam

Does something like this obviate this patch:

head :moved_permanently, :location => book_url(@book)

Or is the sugar of putting the status on redirect_to desired?

Personally, I'm +1 on the sugar.

10/09/07 23:07:40 changed by nzkoz

  • status changed from new to closed.
  • resolution set to fixed.

(In [7820]) Add :status to redirect_to allowing users to choose their own response code without manually setting headers. Closes #8297 [codahale, chasgrundy]