Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix to_query with empty arrays too

  • Loading branch information...
commit c82dbc6a1c1a269fb21de8fd2722fc680ab7ea54 1 parent 7aa4b7d
@rafaelfranca rafaelfranca authored
View
7 activesupport/lib/active_support/core_ext/object/to_query.rb
@@ -18,7 +18,12 @@ class Array
# ['Rails', 'coding'].to_query('hobbies') # => "hobbies%5B%5D=Rails&hobbies%5B%5D=coding"
def to_query(key)
prefix = "#{key}[]"
- collect { |value| value.to_query(prefix) }.join '&'
+
+ if empty?
+ nil.to_query(prefix)
+ else
+ collect { |value| value.to_query(prefix) }.join '&'
+ end
end
end
View
2  activesupport/test/core_ext/object/to_query_test.rb
@@ -55,6 +55,8 @@ def test_nested_empty_hash
{ p: 12, b: { c: false, e: nil, f: '' } }
assert_query_equal 'b%5Bc%5D=3&b%5Bf%5D=&b%5Bk%5D=',
{ b: { c: 3, k: {}, f: '' } }
+ assert_query_equal 'a%5B%5D=&b=3',
+ {a: [], b: 3}
end
private
Please sign in to comment.
Something went wrong with that request. Please try again.