mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Override default_render's behavior with a block
In 0de4a23
the behavior when there is a missing template was changed to
not raise an error, but instead head :no_content. This is a breaking
change and some gems rely on this happening.
To allow gems and other code to work around this, allow
`default_render` to take a block which, if provided, will
execute the contents of that block instead of doing the `head :no_content`.
This commit is contained in:
parent
068ab23a33
commit
6fda6c3778
2 changed files with 38 additions and 2 deletions
|
@ -3,12 +3,27 @@ module ActionController
|
||||||
|
|
||||||
include BasicImplicitRender
|
include BasicImplicitRender
|
||||||
|
|
||||||
|
# Renders the template corresponding to the controller action, if it exists.
|
||||||
|
# The action name, format, and variant are all taken into account.
|
||||||
|
# For example, the "new" action with an HTML format and variant "phone"
|
||||||
|
# would try to render the <tt>new.html+phone.erb</tt> template.
|
||||||
|
#
|
||||||
|
# If no template is found <tt>ActionController::BasicImplicitRender</tt>'s implementation is called, unless
|
||||||
|
# a block is passed. In that case, it will override the super implementation.
|
||||||
|
#
|
||||||
|
# default_render do
|
||||||
|
# head 404 # No template was found
|
||||||
|
# end
|
||||||
def default_render(*args)
|
def default_render(*args)
|
||||||
if template_exists?(action_name.to_s, _prefixes, variants: request.variant)
|
if template_exists?(action_name.to_s, _prefixes, variants: request.variant)
|
||||||
render(*args)
|
render(*args)
|
||||||
else
|
else
|
||||||
logger.info "No template found for #{self.class.name}\##{action_name}, rendering head :no_content" if logger
|
if block_given?
|
||||||
super
|
yield(*args)
|
||||||
|
else
|
||||||
|
logger.info "No template found for #{self.class.name}\##{action_name}, rendering head :no_content" if logger
|
||||||
|
super
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -793,3 +793,24 @@ class RespondToControllerTest < ActionController::TestCase
|
||||||
assert_equal "phone", @response.body
|
assert_equal "phone", @response.body
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class RespondToWithBlockOnDefaultRenderController < ActionController::Base
|
||||||
|
def show
|
||||||
|
default_render do
|
||||||
|
render text: 'default_render yielded'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class RespondToWithBlockOnDefaultRenderControllerTest < ActionController::TestCase
|
||||||
|
def setup
|
||||||
|
super
|
||||||
|
@request.host = "www.example.com"
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_default_render_uses_block_when_no_template_exists
|
||||||
|
get :show
|
||||||
|
assert_equal "default_render yielded", @response.body
|
||||||
|
assert_equal "text/html", @response.content_type
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
Loading…
Reference in a new issue