1

i have the following in my controller

    def invoice_params
      params.require(:invoice).permit({items: [:items]}, :price, :tax, :discount, :sum)
    end

and in my _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]

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**
kostasew
  • 83
  • 8
  • But, it doesn't look like you are sending them to your server as an array. How are you planning to turn a text_field content to an array? – max pleaner Apr 09 '20 at 00:57
  • so sould i replace that with something else ? i have no clue such im new in rails, i thought if i get the same input it will wotk by the time i put in my controller that params[:items] is an array – kostasew Apr 09 '20 at 01:02
  • I never really got the hang of `form_for` (and by extension, all Rails' form-building methods) but I do know at minimum you can do it by writing the form by hand, see https://stackoverflow.com/a/17891423/2981429. Also check out some of the other answers on that question. – max pleaner Apr 09 '20 at 02:33

1 Answers1

3
<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"] } }

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

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>
Thang
  • 811
  • 1
  • 5
  • 12