Skip to content
This repository
Browse code

Raise a RedirectBackError if redirect_to :back is called when theres …

…no HTTP_REFERER defined (closes #3049) [kevin.clark@gmail.com]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3459 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit ea30f7353be3a977c5ea50d8efaa02acf26dd133 1 parent 039a90f
David Heinemeier Hansson authored
2  actionpack/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *SVN*
2 2
 
  3
+* Raise a RedirectBackError if redirect_to :back is called when there's no HTTP_REFERER defined #3049 [kevin.clark@gmail.com]
  4
+
3 5
 * Treat timestamps like datetimes for scaffolding purposes #3388 [Maik Schmidt]
4 6
 
5 7
 * Fix IE bug with link_to "something", :post => true #3443 [Justin Palmer]
9  actionpack/lib/action_controller/base.rb
@@ -40,6 +40,13 @@ def initialize(message = nil)
40 40
       super(message || DEFAULT_MESSAGE)
41 41
     end
42 42
   end
  43
+  class RedirectBackError < ActionControllerError #:nodoc:
  44
+    DEFAULT_MESSAGE = 'No HTTP_REFERER was set in the request to this action, so redirect_to :back could not be called successfully. If this is a test, make sure to specify @request.env["HTTP_REFERER"].'
  45
+  
  46
+    def initialize(message = nil)
  47
+      super(message || DEFAULT_MESSAGE)
  48
+    end
  49
+  end
43 50
 
44 51
   # Action Controllers are made up of one or more actions that performs its purpose and then either renders a template or
45 52
   # redirects to another action. An action is defined as a public method on the controller, which will automatically be 
@@ -780,7 +787,7 @@ def redirect_to(options = {}, *parameters_for_method_reference) #:doc:
780 787
             redirect_to(request.protocol + request.host_with_port + options)
781 788
           
782 789
           when :back
783  
-            redirect_to(request.env["HTTP_REFERER"])
  790
+            request.env["HTTP_REFERER"] ? redirect_to(request.env["HTTP_REFERER"]) : raise(RedirectBackError)
784 791
 
785 792
           else
786 793
             if parameters_for_method_reference.empty?
9  actionpack/test/controller/redirect_test.rb
@@ -27,6 +27,8 @@ def redirect_to_back
27 27
   end
28 28
 
29 29
   def rescue_errors(e) raise e end
  30
+    
  31
+  def rescue_action(e) raise end
30 32
   
31 33
   protected
32 34
     def dashbord_url(id, message)
@@ -76,6 +78,13 @@ def test_redirect_to_back
76 78
     get :redirect_to_back
77 79
     assert_redirect_url "http://www.example.com/coming/from"
78 80
   end
  81
+  
  82
+  def test_redirect_to_back_with_no_referer
  83
+    assert_raises(ActionController::RedirectBackError) {
  84
+      @request.env["HTTP_REFERER"] = nil
  85
+      get :redirect_to_back
  86
+    }
  87
+  end
79 88
 end
80 89
 
81 90
 module ModuleTest
4  actionpack/test/template/url_helper_test.rb
@@ -110,14 +110,14 @@ def test_link_tag_with_popup_and_javascript_confirm
110 110
   
111 111
   def test_link_tag_using_post_javascript
112 112
     assert_dom_equal(
113  
-      "<a href=\"http://www.example.com\" onclick=\"f = document.createElement('form'); document.body.appendChild(f); f.method = 'POST'; f.action = this.href; f.submit();return false;\">Hello</a>",
  113
+      "<a href=\"http://www.example.com\" onclick=\"var f = document.createElement('form'); document.body.appendChild(f); f.method = 'POST'; f.action = this.href; f.submit();return false;\">Hello</a>",
114 114
       link_to("Hello", "http://www.example.com", :post => true)
115 115
     )
116 116
   end
117 117
   
118 118
   def test_link_tag_using_post_javascript_and_confirm
119 119
     assert_dom_equal(
120  
-      "<a href=\"http://www.example.com\" onclick=\"if (confirm('Are you serious?')) { f = document.createElement('form'); document.body.appendChild(f); f.method = 'POST'; f.action = this.href; f.submit(); };return false;\">Hello</a>",
  120
+      "<a href=\"http://www.example.com\" onclick=\"if (confirm('Are you serious?')) { var f = document.createElement('form'); document.body.appendChild(f); f.method = 'POST'; f.action = this.href; f.submit(); };return false;\">Hello</a>",
121 121
       link_to("Hello", "http://www.example.com", :post => true, :confirm => "Are you serious?")
122 122
     )    
123 123
   end

0 notes on commit ea30f73

Please sign in to comment.
Something went wrong with that request. Please try again.