1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix to_query with empty arrays too

This commit is contained in:
Rafael Mendonça França 2014-02-06 01:03:41 -02:00
parent 7aa4b7dc6b
commit c82dbc6a1c
2 changed files with 8 additions and 1 deletions

View file

@ -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 file

@ -55,6 +55,8 @@ class ToQueryTest < ActiveSupport::TestCase
{ 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