diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 2923232021134a83d6a1f536bdd68b226e0f7eab..69075f04db61bfc8bbebfb942cf689ee9aa5de77 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,10 @@ *SVN* +* Added arrays as a value option for params in url_for and friends #467 [Eric Anderson]. Example: + + url_for(:controller => 'user', :action => 'delete', :params => { 'username' => %( paul john steve ) } ) + # => /user/delete?username[]=paul&username[]=john&username[]=steve + * Fixed that controller tests can now assert on the use of cookies #466 [Alexey] * Fixed that send_file would "remember" all the files sent by adding to the headers again and again #458 [bitsweat] diff --git a/actionpack/lib/action_controller/url_rewriter.rb b/actionpack/lib/action_controller/url_rewriter.rb index 7bf9015b377b8c9c7e67538442fb32aa113f1279..fa003119ece77d70fb9ede61e453745cfd81d9c5 100644 --- a/actionpack/lib/action_controller/url_rewriter.rb +++ b/actionpack/lib/action_controller/url_rewriter.rb @@ -174,7 +174,12 @@ def build_query_string(hash) elements = [] query_string = "" - hash.each { |key, value| elements << "#{CGI.escape(key)}=#{CGI.escape(value.to_s)}" } + hash.each do |key, value| + key = CGI.escape key + key += '[]' if value.class == Array + value = [ value ] unless value.class == Array + value.each { |val| elements << "#{key}=#{CGI.escape(val.to_s)}" } + end unless elements.empty? then query_string << ("?" + elements.join("&")) end return query_string diff --git a/actionpack/test/controller/url_test.rb b/actionpack/test/controller/url_test.rb index b595b1c25c5516e55639a6cbf864bef37f945781..8315830776c4557d0ebaf70dd3c0cf30dbb1a739 100644 --- a/actionpack/test/controller/url_test.rb +++ b/actionpack/test/controller/url_test.rb @@ -246,7 +246,18 @@ def test_parameters_with_id ) end end - + + def test_parameters_with_array + @clean_urls.each do |url| + assert_equal( + "http://www.singlefile.com/identity/show?id[]=3&id[]=5&id[]=10", + url.rewrite( + :action => "show", + :params => { 'id' => [ 3, 5, 10 ] } ) + ) + end + end + def test_action_with_id assert_equal( "http://www.singlefile.com/identity/show/7",