aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_controller/base.rb
diff options
context:
space:
mode:
authorMichael Koziarski <michael@koziarski.com>2007-10-09 23:07:36 +0000
committerMichael Koziarski <michael@koziarski.com>2007-10-09 23:07:36 +0000
commit4aabe46341dd6db9bf9bbbbc27c32f467a7c55b7 (patch)
tree30a7dc0bf2b0e3756caba87556ef340071769a5b /actionpack/lib/action_controller/base.rb
parent19c9c7fafbc7c808c080317a1326c4fbc6068bea (diff)
downloadrails-4aabe46341dd6db9bf9bbbbc27c32f467a7c55b7.tar.gz
rails-4aabe46341dd6db9bf9bbbbc27c32f467a7c55b7.tar.bz2
rails-4aabe46341dd6db9bf9bbbbc27c32f467a7c55b7.zip
Add :status to redirect_to allowing users to choose their own response code without manually setting headers. Closes #8297 [codahale, chasgrundy]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7820 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib/action_controller/base.rb')
-rwxr-xr-xactionpack/lib/action_controller/base.rb31
1 files changed, 23 insertions, 8 deletions
diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb
index 719593c207..0f5604c707 100755
--- a/actionpack/lib/action_controller/base.rb
+++ b/actionpack/lib/action_controller/base.rb
@@ -987,32 +987,47 @@ module ActionController #:nodoc:
# redirect_to "/images/screenshot.jpg"
# redirect_to :back
#
- # The redirection happens as a "302 Moved" header.
+ # 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 <tt>redirect_to :back</tt>, if there is no referrer,
# RedirectBackError will be raised. You may specify some fallback
# behavior for this case by rescuing RedirectBackError.
- def redirect_to(options = {}) #:doc:
+ def redirect_to(options = {}, response_status = {}) #:doc:
+
+ if options.is_a?(Hash) && options[:status]
+ status = options.delete(:status)
+ elsif response_status[:status]
+ status = response_status[:status]
+ else
+ status = 302
+ end
+
case options
when %r{^\w+://.*}
raise DoubleRenderError if performed?
- logger.info("Redirected to #{options}") if logger
- response.redirect(options)
+ logger.info("Redirected to #{options}") if logger && logger.info?
+ response.redirect(options, interpret_status(status))
response.redirected_to = options
@performed_redirect = true
when String
- redirect_to(request.protocol + request.host_with_port + options)
+ redirect_to(request.protocol + request.host_with_port + options, :status=>status)
when :back
- request.env["HTTP_REFERER"] ? redirect_to(request.env["HTTP_REFERER"]) : raise(RedirectBackError)
+ request.env["HTTP_REFERER"] ? redirect_to(request.env["HTTP_REFERER"], :status=>status) : raise(RedirectBackError)
when Hash
- redirect_to(url_for(options))
+ redirect_to(url_for(options), :status=>status)
response.redirected_to = options
else
- redirect_to(url_for(options))
+ redirect_to(url_for(options), :status=>status)
end
end