提交 326e6527 编写于 作者: H Hincu Petru 提交者: Rafael Mendonça França

Fixed "Hash#to_param confused by empty hash values #13892"

上级 6cc3afb0
......@@ -51,6 +51,7 @@ class Hash
#
# This method is also aliased as +to_query+.
def to_param(namespace = nil)
return (namespace ? nil.to_query(namespace) : '') if empty?
collect do |key, value|
value.to_query(namespace ? "#{namespace}[#{key}]" : key)
end.sort! * '&'
......
require 'abstract_unit'
require 'active_support/core_ext/object/to_param'
require 'active_support/core_ext/object/to_query'
class ToParamTest < ActiveSupport::TestCase
def test_object
......@@ -16,4 +17,14 @@ def test_boolean
assert_equal true, true.to_param
assert_equal false, false.to_param
end
def test_nested_empty_hash
hash1 = {a: 1, b: {c: 3, d: {}}}.to_param
hash2 = {p: 12, b: {c: 3, e: nil, f: ''}}.to_param
hash3 = {b: {c: 3, k: {}, f: '' }}.to_param
assert_equal 'a=1&b[c]=3&b[d]=', CGI::unescape(hash1)
assert_equal 'b[c]=3&b[e]=&b[f]=&p=12', CGI::unescape(hash2)
assert_equal 'b[c]=3&b[f]=&b[k]=', CGI::unescape(hash3)
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册