提交 ca4417d3 编写于 作者: D David Heinemeier Hansson

Merge pull request #19377 from sb8244/issue-19036

when a template is missing for the default render, do head no_content instead
* For actions with no corresponding templates, render `head :no_content`
instead of raising an error. This allows for slimmer API controller
methods that simply work, without needing further instructions.
See #19036.
*Stephen Bussey*
* Provide friendlier access to request variants.
request.variant = :phone
......
......@@ -7,7 +7,12 @@ def send_action(method, *args)
end
def default_render(*args)
render(*args)
if template_exists?(action_name.to_s, _prefixes, variants: request.variant)
render(*args)
else
logger.info "No template found for #{self.class.name}\##{action_name}, rendering head :no_content"
head :no_content
end
end
def method_for_action(action_name)
......
......@@ -608,19 +608,29 @@ def test_invalid_format
end
def test_invalid_variant
logger = ActiveSupport::LogSubscriber::TestHelper::MockLogger.new
old_logger, ActionController::Base.logger = ActionController::Base.logger, logger
@request.variant = :invalid
assert_raises(ActionView::MissingTemplate) do
get :variant_with_implicit_rendering
end
get :variant_with_implicit_rendering
assert_response :no_content
assert_equal 1, logger.logged(:info).select{ |s| s =~ /No template found/ }.size, "Implicit head :no_content not logged"
ensure
ActionController::Base.logger = old_logger
end
def test_variant_not_set_regular_template_missing
assert_raises(ActionView::MissingTemplate) do
get :variant_with_implicit_rendering
end
get :variant_with_implicit_rendering
assert_response :no_content
end
def test_variant_with_implicit_rendering
@request.variant = :implicit
get :variant_with_implicit_rendering
assert_response :no_content
end
def test_variant_with_implicit_template_rendering
@request.variant = :mobile
get :variant_with_implicit_rendering
assert_equal "text/html", @response.content_type
......
......@@ -126,7 +126,7 @@ def find_all(name, prefixes = [], partial = false, keys = [], options = {})
@view_paths.find_all(*args_for_lookup(name, prefixes, partial, keys, options))
end
def exists?(name, prefixes = [], partial = false, keys = [], options = {})
def exists?(name, prefixes = [], partial = false, keys = [], **options)
@view_paths.exists?(*args_for_lookup(name, prefixes, partial, keys, options))
end
alias :template_exists? :exists?
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册