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:
parent
7aa4b7dc6b
commit
c82dbc6a1c
2 changed files with 8 additions and 1 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue