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
This commit is contained in:
David Heinemeier Hansson
2006-01-22 01:52:23 +00:00
parent 039a90f535
commit ea30f7353b
4 changed files with 21 additions and 3 deletions

View File

@@ -1,5 +1,7 @@
*SVN*
* Raise a RedirectBackError if redirect_to :back is called when there's no HTTP_REFERER defined #3049 [kevin.clark@gmail.com]
* Treat timestamps like datetimes for scaffolding purposes #3388 [Maik Schmidt]
* Fix IE bug with link_to "something", :post => true #3443 [Justin Palmer]

View File

@@ -40,6 +40,13 @@ module ActionController #:nodoc:
super(message || DEFAULT_MESSAGE)
end
end
class RedirectBackError < ActionControllerError #:nodoc:
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"].'
def initialize(message = nil)
super(message || DEFAULT_MESSAGE)
end
end
# Action Controllers are made up of one or more actions that performs its purpose and then either renders a template or
# redirects to another action. An action is defined as a public method on the controller, which will automatically be
@@ -780,7 +787,7 @@ module ActionController #:nodoc:
redirect_to(request.protocol + request.host_with_port + options)
when :back
redirect_to(request.env["HTTP_REFERER"])
request.env["HTTP_REFERER"] ? redirect_to(request.env["HTTP_REFERER"]) : raise(RedirectBackError)
else
if parameters_for_method_reference.empty?

View File

@@ -27,6 +27,8 @@ class RedirectController < ActionController::Base
end
def rescue_errors(e) raise e end
def rescue_action(e) raise end
protected
def dashbord_url(id, message)
@@ -76,6 +78,13 @@ class RedirectTest < Test::Unit::TestCase
get :redirect_to_back
assert_redirect_url "http://www.example.com/coming/from"
end
def test_redirect_to_back_with_no_referer
assert_raises(ActionController::RedirectBackError) {
@request.env["HTTP_REFERER"] = nil
get :redirect_to_back
}
end
end
module ModuleTest

View File

@@ -110,14 +110,14 @@ class UrlHelperTest < Test::Unit::TestCase
def test_link_tag_using_post_javascript
assert_dom_equal(
"<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>",
"<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>",
link_to("Hello", "http://www.example.com", :post => true)
)
end
def test_link_tag_using_post_javascript_and_confirm
assert_dom_equal(
"<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>",
"<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>",
link_to("Hello", "http://www.example.com", :post => true, :confirm => "Are you serious?")
)
end