我正试图从我的参数中获取一系列对象,寻找这样的东西:
"invoice"=>{"items"=>[{"description"=>"[no description]", "units"=>"0", "rate"=>"0"}, {"description"=>"[no description]", "units"=>"0", "rate"=>"0"}]}
我正在设置我的表格:
<input type="hidden" name="invoice[invoice_items][description]" />
<input type="hidden" name="invoice[invoice_items][units]"/>
<input type="hidden" name="invoice[invoice_items][rate]" />
输入动态添加,因此每个对象有3个组;从Rails中的表单发送对象数组的正确方法是什么?
表单请求的结果如下所示:
"invoice"=>{"items"=>{"description"=>"[no description]", "units"=>"0", "rate"=>"0"}}
即使我添加了更多项目,也总是有第一项的哈希
答案 0 :(得分:3)
尝试添加方括号:
<input type="hidden" name="invoice[items][][description]">
<input type="hidden" name="invoice[items][][units]">
<input type="hidden" name="invoice[items][][rate]">
Rails会将这些参数解析为:
"invoice" => {
"items"=> [
{ "description" => "", "units" => "", "rate" => "" },
{ "description" => "", "units" => "", "rate" => "" }
]
}