Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby循环数组_Ruby_Arrays_Loops - Fatal编程技术网

Ruby循环数组

Ruby循环数组,ruby,arrays,loops,Ruby,Arrays,Loops,在表格中,我有一个来自数据库的产品列表,每个产品在表格中都有以下项目: = text_field_tag 'item['+product.id.to_s+'][]' = text_field_tag 'item_value1['+product.id.to_s+'][]' = text_field_tag 'item_value2['+product.id.to_s+'][]' 我正在尝试循环数组并通过以下方式获取所有(项目,项目_值1,项目_值2): params[:item].each_w

在表格中,我有一个来自数据库的产品列表,每个产品在表格中都有以下项目:

= text_field_tag 'item['+product.id.to_s+'][]'
= text_field_tag 'item_value1['+product.id.to_s+'][]'
= text_field_tag 'item_value2['+product.id.to_s+'][]'
我正在尝试循环数组并通过以下方式获取所有(项目项目_值1项目_值2):

params[:item].each_with_index do |val, index|
  puts "#{val[0]} => #{val[1]}"
end
输出如下所示:

191359 => [""]
191361 => [""]
191360 => ["15"]
191212 => [""]
191210 => ["9"]
248974 => [""]
191209 => [""]
190920 => [""]
190919 => [""]
190921 => [""]
但是,如何获取各个产品的所有数据?差不多

puts "item: #{item}, item_value1: #{item_value1}, item_value2: #{item_value2}"

这里有三个参数
item
item\u value1
item\u value2
。迭代项将仅从参数项获得值,而不是从
intem\u value1
item\u value2
获得值。如果这3个参数的索引是相对的,那么您可以在代码中使用索引

params[:item].each_with_index do |val, index|
  puts "#{params[:item_value1][index]} => #{params[:item_value1][index]}"
end