0

在一个表单中,我有一个来自数据库的产品列表,每个产品在表单中都有这些项目:

= 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+'][]'

我正在尝试遍历数组并以这种方式获取所有这些(itemitem_value1item_value2):

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}"
4

1 回答 1

2

这里有三个item参数item_value1item_value2。迭代 item 只会给你来自参数 items 的值,而不是来自intem_value1and item_value2。如果这 3 个参数的索引是相对的,那么您可以在代码中使用 index

params[:item].each_with_index do |val, index|
  puts "#{params[:item_value1][index]} => #{params[:item_value1][index]}"
end
于 2012-09-12T14:23:29.793 回答