开发者

Indexed multi-value params in rails

开发者 https://www.devze.com 2023-03-29 06:07 出处:网络
I have a form like <input name=\"url[0]\" type=\"text\" /> <input name=\"url[1]\" type=\"text\" />

I have a form like

<input name="url[0]" type="text" />
<input name="url[1]" type="text" />
<input name="url[2]" type="text" />

I would like to be able to access these like:

params[:url].each do |url|
  # work
end

I know that if I remove the explicit index from the name, this will work, but I would prefer 开发者_如何学JAVAto keep the index in. Is this something supported by rails out of the box?


You need to modify your block like so:

params[:url].each do |index, url|
  # work
end


params[:url] will be an hash like this:

params[:url]
 => {'0' => 'url-1', '1' => 'url-2', '2' => 'url-3'}

So you have to iterate over the hash like this:

params[:url].each do |key, value|
 # work
end
0

精彩评论

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