aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHincu Petru <hincupetru@gmail.com>2014-02-03 09:51:05 +0000
committerRafael Mendonça França <rafaelmfranca@gmail.com>2014-02-05 23:31:50 -0200
commit326e6527497126b2ea3627e377b6a4b5c9191bef (patch)
tree8d13260b7ad28b511a6f5b0ec02c945497033416
parent6cc3afb00a0d7689f5435a3845fd17266d935c3e (diff)
downloadrails-326e6527497126b2ea3627e377b6a4b5c9191bef.tar.gz
rails-326e6527497126b2ea3627e377b6a4b5c9191bef.tar.bz2
rails-326e6527497126b2ea3627e377b6a4b5c9191bef.zip
Fixed "Hash#to_param confused by empty hash values #13892"
-rw-r--r--activesupport/lib/active_support/core_ext/object/to_param.rb1
-rw-r--r--activesupport/test/core_ext/object/to_param_test.rb11
2 files changed, 12 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/object/to_param.rb b/activesupport/lib/active_support/core_ext/object/to_param.rb
index 3b137ce6ae..e40846e7d6 100644
--- a/activesupport/lib/active_support/core_ext/object/to_param.rb
+++ b/activesupport/lib/active_support/core_ext/object/to_param.rb
@@ -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! * '&'
diff --git a/activesupport/test/core_ext/object/to_param_test.rb b/activesupport/test/core_ext/object/to_param_test.rb
index bd7c6c422a..eae68ed184 100644
--- a/activesupport/test/core_ext/object/to_param_test.rb
+++ b/activesupport/test/core_ext/object/to_param_test.rb
@@ -1,5 +1,6 @@
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 @@ class ToParamTest < ActiveSupport::TestCase
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