From 4f754985d0d39a9e7831e45809b030b2b3c62d09 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 2 Nov 2005 14:47:03 +0000 Subject: Added redirect_to :back as a short-hand for redirect_to(request.env["HTTP_REFERER"]) git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2848 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 2 ++ actionpack/lib/action_controller/base.rb | 6 ++++++ actionpack/test/controller/redirect_test.rb | 10 ++++++++++ 3 files changed, 18 insertions(+) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index aafe80ed9a..c339adfc1d 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Added redirect_to :back as a short-hand for redirect_to(request.env["HTTP_REFERER"]) + * Change javascript_include_tag :defaults to not use script.aculo.us loader, which facilitates the use of plugins for future script.aculo.us and third party javascript extensions, and provide register_javascript_include_default for plugins to specify additional JavaScript files to load. Removed slider.js and builder.js from actionpack. [Thomas Fuchs] * Fix problem where redirecting components can cause an infinite loop [Rick Olson] diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index 7c2f7239fa..07b9b443d2 100755 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -727,11 +727,14 @@ module ActionController #:nodoc: # * 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 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. def redirect_to(options = {}, *parameters_for_method_reference) #:doc: @@ -745,6 +748,9 @@ module ActionController #:nodoc: when String redirect_to(request.protocol + request.host_with_port + options) + + when :back + redirect_to(request.env["HTTP_REFERER"]) else if parameters_for_method_reference.empty? diff --git a/actionpack/test/controller/redirect_test.rb b/actionpack/test/controller/redirect_test.rb index f18f3682de..4bad5e26ca 100755 --- a/actionpack/test/controller/redirect_test.rb +++ b/actionpack/test/controller/redirect_test.rb @@ -22,6 +22,10 @@ class RedirectController < ActionController::Base redirect_to :action => "hello_world" end + def redirect_to_back + redirect_to :back + end + def rescue_errors(e) raise e end protected @@ -66,6 +70,12 @@ class RedirectTest < Test::Unit::TestCase get :redirect_with_assigns assert_equal "world", assigns["hello"] end + + def test_redirect_to_back + @request.env["HTTP_REFERER"] = "http://www.example.com/coming/from" + get :redirect_to_back + assert_redirect_url "http://www.example.com/coming/from" + end end module ModuleTest -- cgit v1.2.3