Skip to content
This repository
Browse code

Added arrays as a value option for params in url_for and friends #467

…[Eric Anderson]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@403 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 9c09f81bc6bc08122e2835fb59e66d80751bd058 1 parent 1d61071
David Heinemeier Hansson dhh authored
5 actionpack/CHANGELOG
... ... @@ -1,5 +1,10 @@
1 1 *SVN*
2 2
  3 +* Added arrays as a value option for params in url_for and friends #467 [Eric Anderson]. Example:
  4 +
  5 + url_for(:controller => 'user', :action => 'delete', :params => { 'username' => %( paul john steve ) } )
  6 + # => /user/delete?username[]=paul&username[]=john&username[]=steve
  7 +
3 8 * Fixed that controller tests can now assert on the use of cookies #466 [Alexey]
4 9
5 10 * Fixed that send_file would "remember" all the files sent by adding to the headers again and again #458 [bitsweat]
7 actionpack/lib/action_controller/url_rewriter.rb
@@ -174,7 +174,12 @@ def build_query_string(hash)
174 174 elements = []
175 175 query_string = ""
176 176
177   - hash.each { |key, value| elements << "#{CGI.escape(key)}=#{CGI.escape(value.to_s)}" }
  177 + hash.each do |key, value|
  178 + key = CGI.escape key
  179 + key += '[]' if value.class == Array
  180 + value = [ value ] unless value.class == Array
  181 + value.each { |val| elements << "#{key}=#{CGI.escape(val.to_s)}" }
  182 + end
178 183 unless elements.empty? then query_string << ("?" + elements.join("&")) end
179 184
180 185 return query_string
13 actionpack/test/controller/url_test.rb
@@ -246,7 +246,18 @@ def test_parameters_with_id
246 246 )
247 247 end
248 248 end
249   -
  249 +
  250 + def test_parameters_with_array
  251 + @clean_urls.each do |url|
  252 + assert_equal(
  253 + "http://www.singlefile.com/identity/show?id[]=3&id[]=5&id[]=10",
  254 + url.rewrite(
  255 + :action => "show",
  256 + :params => { 'id' => [ 3, 5, 10 ] } )
  257 + )
  258 + end
  259 + end
  260 +
250 261 def test_action_with_id
251 262 assert_equal(
252 263 "http://www.singlefile.com/identity/show/7",

0 comments on commit 9c09f81

Please sign in to comment.
Something went wrong with that request. Please try again.