开发者

Are array parameters in rails guaranteed to be in the order in which they appear in the url?

开发者 https://www.devze.com 2023-01-17 21:53 出处:网络
Given the following url: http://example.com?arr[]=hello&arr[]=to&arr[]=you Am I able to bank on the fact that:

Given the following url: http://example.com?arr[]=hello&arr[]=to&arr[]=you

Am I able to bank on the fact that:

params[:arr] == ['hello', 'to', 'you']

?

I ask because I hav开发者_如何转开发e some additional data that will be sent with the request that needs to be mapped to each of the values in params[:arr].


Yes, they are.

Well, maybe a prove from the code where URL parameters are parsed would be handy (I've ommited some code from the example):

#
# file: ../rack-1.2.1/lib/rack/utils.rb
#

def normalize_params(params, name, v = nil)

  # code ommited for simplicity...

  if after == ""
    params[k] = v
  elsif after == "[]"
    params[k] ||= []
    # HERE IT IS!
    params[k] << v
  elsif
  # code ommited for simplicity...
  # ...
end

well, you should take a look yourself but as you can see, the crucial part is where values are simply added to the array - this operation will keep the order.

0

精彩评论

暂无评论...
验证码 换一张
取 消