提交 8058ece4 编写于 作者: A Andrew White 提交者: GitHub

Merge pull request #26555 from chriscarter90/unmatched-constraint-routing-messages

Show an "unmatched constraints" error when params fail to match constraints
* Show an "unmatched constraints" error when params fail to match constraints
on a matched route, rather than a "missing keys" error.
Fixes #26470.
*Chris Carter*
* Fix adding implicitly rendered template digests to ETags.
Fixes a case when modifying an implicitly rendered template for a
......
......@@ -44,8 +44,12 @@ def generate(name, options, path_parameters, parameterize = nil)
return [route.format(parameterized_parts), params]
end
unmatched_keys = (missing_keys || []) & constraints.keys
missing_keys = (missing_keys || []) - unmatched_keys
message = "No route matches #{Hash[constraints.sort_by { |k,v| k.to_s }].inspect}"
message << " missing required keys: #{missing_keys.sort.inspect}" if missing_keys && !missing_keys.empty?
message << ", missing required keys: #{missing_keys.sort.inspect}" if missing_keys && !missing_keys.empty?
message << ", possible unmatched constraints: #{unmatched_keys.sort.inspect}" if unmatched_keys && !unmatched_keys.empty?
raise ActionController::UrlGenerationError, message
end
......
......@@ -210,7 +210,7 @@ def raise_generation_error(args)
}
constraints = Hash[@route.requirements.merge(params).sort_by { |k,v| k.to_s }]
message = "No route matches #{constraints.inspect}"
message << " missing required keys: #{missing_keys.sort.inspect}"
message << ", missing required keys: #{missing_keys.sort.inspect}"
raise ActionController::UrlGenerationError, message
end
......
......@@ -4684,22 +4684,25 @@ def app; APP end
include Routes.url_helpers
test "url helpers raise a helpful error message when generation fails" do
test "url helpers raise a 'missing keys' error for a nil param with optimized helpers" do
url, missing = { action: "show", controller: "products", id: nil }, [:id]
message = "No route matches #{url.inspect} missing required keys: #{missing.inspect}"
message = "No route matches #{url.inspect}, missing required keys: #{missing.inspect}"
# Optimized url helper
error = assert_raises(ActionController::UrlGenerationError) { product_path(nil) }
assert_equal message, error.message
end
test "url helpers raise a 'constraint failure' error for a nil param with non-optimized helpers" do
url, missing = { action: "show", controller: "products", id: nil }, [:id]
message = "No route matches #{url.inspect}, possible unmatched constraints: #{missing.inspect}"
# Non-optimized url helper
error = assert_raises(ActionController::UrlGenerationError, message) { product_path(id: nil) }
assert_equal message, error.message
end
test "url helpers raise message with mixed parameters when generation fails " do
test "url helpers raise message with mixed parameters when generation fails" do
url, missing = { action: "show", controller: "products", id: nil, "id"=>"url-tested" }, [:id]
message = "No route matches #{url.inspect} missing required keys: #{missing.inspect}"
message = "No route matches #{url.inspect}, possible unmatched constraints: #{missing.inspect}"
# Optimized url helper
error = assert_raises(ActionController::UrlGenerationError) { product_path(nil, "id"=>"url-tested") }
......
......@@ -297,7 +297,7 @@ def test_generate_missing_keys_no_matches_different_format_keys
}
request_parameters = primarty_parameters.merge(redirection_parameters).merge(missing_parameters)
message = "No route matches #{Hash[request_parameters.sort_by { |k,v|k.to_s }].inspect} missing required keys: #{[missing_key.to_sym].inspect}"
message = "No route matches #{Hash[request_parameters.sort_by { |k,v|k.to_s }].inspect}, missing required keys: #{[missing_key.to_sym].inspect}"
error = assert_raises(ActionController::UrlGenerationError) do
@formatter.generate(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册