假设我有一个用于添加新产品的表单。每种产品可能有多种尺寸,其中每种尺寸可能有多种选择。表格看起来像这样:
$(elm).html('');
我的目的是在控制器中接收嵌套地图以处理数据,我希望收到这样的地图:
<form accept-charset="UTF-8" action="/products?_format=json" method="post">
<input id="product_name" name="product[name]" type="text">
<input id="product_product_sizes_name" name="product[product_sizes][][name]" type="text">
<input id="product_product_sizes_product_size_options_name" name="product[product_sizes][][product_size_options][][name]" type="text">
<input id="product_product_sizes_product_size_options_size" name="product[product_sizes][][product_size_options][][size]" type="text">
<input id="product_product_materials_name" name="product[product_materials][][name]" type="text">
<button type="submit"> Create </button>
</form>
但我得到的是:
%{
"product" =>
%{
"name" => "value",
"product_materials" => [
%{
"name" => "value"
}
],
"product_sizes" => [
%{
"name" => "value",
"product_size_options" => [
%{
"name" => "value",
"size" => "value"
}
]
}
]
}
}
所以我想知道我是否在这里做错了,实际上可以更改表单名称并接收我期望的地图,或者不是这种情况,我应该使用显式索引(如{{1}中所示) },%{
"product" =>
%{
"name" => "value",
"product_materials" => [
%{
"name" => ""}
],
"product_sizes" => [
%{
"name" => ""
},
%{
"product_size_options" => [
%{
"name" => ""
}
]
},
%{
"product_size_options" => [
%{
"size" => ""
}
]
}
]
}
}
等等而不是[0]
)或类似的东西,并在提交后手动合并数据?