Indexed multi-value params in rails
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
精彩评论