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