[英]how do i get params as array in ruby on rails?
i have the following in my controller我的 controller 中有以下内容
def invoice_params
params.require(:invoice).permit({items: [:items]}, :price, :tax, :discount, :sum)
end
and in my _form在我的_form中
<td><input size="8" <% form.text_field :items %> /></td> [pants]
<td><input size="8" <%= form.text_field :items %> /></td> [spoon]
<td><input size="8" <%= form.text_field :items %>/></td> [nil]
<td><input size="8" <%= form.text_field :items %> /></td> [nil]
<td><input size="8" /></td>
so i want my array be like ["pants","Spoon",nil,nil]
所以我希望我的数组像["pants","Spoon",nil,nil]
with that style i have the following error in my server使用这种风格,我的服务器出现以下错误
Started POST "/invoices" for 10.0.2.1 at 2020-04-09 00:46:55 +0000
Cannot render console from 10.0.2.1! Allowed networks: 127.0.0.0/127.255.255.255, ::1
Processing by InvoicesController#create as HTML
Parameters: {"authenticity_token"=>"94T3Stght1p9n9Bo44+lvS7CMnE5PEkuic5DP8ifYH4yxY5roVt19ZHpL6sIaiO31i/S5DADP+/ffNG3wdobLQ==", "invoice"=>{"items"=>"das", "tax"=>""}, "commit"=>"Create Invoice"}
**Unpermitted parameter: :items**
<input type="text" name="invoice[items][]" value="pants">
<input type="text" name="invoice[items][]" value="spoon">
Will give you the params as an ARRAY: { invoice: { items: ["pant", "spoon"] } }
将为您提供参数作为数组: { invoice: { items: ["pant", "spoon"] } }
<input type="text" name="invoice[items][1]" value="pants">
<input type="text" name="invoice[items][2]" value="spoon">
Will give you the params as a HASH: { invoice: { items: { "1": "pant", "2": "spoon" } } }
会给你的参数为 HASH: { invoice: { items: { "1": "pant", "2": "spoon" } } }
For your problem, you need to specify the name manually like below:对于您的问题,您需要手动指定名称,如下所示:
<td><%= form.text_field name: "invoice[items][]" %> /></td>
<td><%= form.text_field name: "invoice[items][]" %> /></td>
<td><%= form.text_field name: "invoice[items][]" %> /></td>
<td><%= form.text_field name: "invoice[items][]" %> /></td>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.