提交 82668678 编写于 作者: J Jamis Buck

Improved error message for DoubleRenderError


git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1779 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 4bed0ab9
*SVN* *SVN*
* Improved error message for DoubleRenderError
* Fixed routing to allow for testing of *path components #1650 [Nicholas Seckar] * Fixed routing to allow for testing of *path components #1650 [Nicholas Seckar]
* Added :handle as an option to sortable_element to restrict the drag handle to a given class #1642 [thejohnny] * Added :handle as an option to sortable_element to restrict the drag handle to a given class #1642 [thejohnny]
......
...@@ -26,6 +26,11 @@ class UnknownAction < ActionControllerError #:nodoc: ...@@ -26,6 +26,11 @@ class UnknownAction < ActionControllerError #:nodoc:
class MissingFile < ActionControllerError #:nodoc: class MissingFile < ActionControllerError #:nodoc:
end end
class DoubleRenderError < ActionControllerError #:nodoc: class DoubleRenderError < ActionControllerError #:nodoc:
DEFAULT_MESSAGE = "Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and only once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like \"redirect_to(...) and return\". Finally, note that to cause a before filter to halt execution of the rest of the filter chain, the filter must return false, explicitly, so \"render(...) and return false\"."
def initialize(message=nil)
super(message || DEFAULT_MESSAGE)
end
end end
# Action Controllers are made up of one or more actions that performs its purpose and then either renders a template or # Action Controllers are made up of one or more actions that performs its purpose and then either renders a template or
...@@ -560,7 +565,7 @@ def controller_name ...@@ -560,7 +565,7 @@ def controller_name
# render :nothing => true, :status => 401 # render :nothing => true, :status => 401
def render(options = {}, deprecated_status = nil) #:doc: def render(options = {}, deprecated_status = nil) #:doc:
# puts "Rendering: #{options.inspect}" # puts "Rendering: #{options.inspect}"
raise DoubleRenderError, "Can only render or redirect once per action" if performed? raise DoubleRenderError if performed?
# Backwards compatibility # Backwards compatibility
return render({ :template => options || default_template_name, :status => deprecated_status }) if !options.is_a?(Hash) return render({ :template => options || default_template_name, :status => deprecated_status }) if !options.is_a?(Hash)
...@@ -684,7 +689,7 @@ def default_url_options(options) #:doc: ...@@ -684,7 +689,7 @@ def default_url_options(options) #:doc:
def redirect_to(options = {}, *parameters_for_method_reference) #:doc: def redirect_to(options = {}, *parameters_for_method_reference) #:doc:
case options case options
when %r{^\w+://.*} when %r{^\w+://.*}
raise DoubleRenderError, "Can only render or redirect once per action" if performed? raise DoubleRenderError if performed?
logger.info("Redirected to #{options}") unless logger.nil? logger.info("Redirected to #{options}") unless logger.nil?
response.redirect(options) response.redirect(options)
response.redirected_to = options response.redirected_to = options
......
...@@ -99,6 +99,21 @@ def render_with_explicit_template ...@@ -99,6 +99,21 @@ def render_with_explicit_template
render "test/hello_world" render "test/hello_world"
end end
def double_render
render :text => "hello"
render :text => "world"
end
def double_redirect
redirect_to :action => "double_render"
redirect_to :action => "double_render"
end
def render_and_redirect
render :text => "hello"
redirect_to :action => "double_render"
end
def rescue_action(e) raise end def rescue_action(e) raise end
private private
...@@ -260,4 +275,16 @@ def test_render_with_explicit_template ...@@ -260,4 +275,16 @@ def test_render_with_explicit_template
get :render_with_explicit_template get :render_with_explicit_template
assert_response :success assert_response :success
end end
def test_double_render
assert_raises(ActionController::DoubleRenderError) { get :double_render }
end
def test_double_redirect
assert_raises(ActionController::DoubleRenderError) { get :double_redirect }
end
def test_render_and_redirect
assert_raises(ActionController::DoubleRenderError) { get :render_and_redirect }
end
end end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册