开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜