如何 post 在 Rails 的表单中对输入数据进行分组
How to post grouped input data within a form in Rails
我有一个包含 4 个输入字段的表单。
<%= semantic_form_for @some_model, html: {class: "horizontal-form"} do |f| %>
<%= f.input :person_id %>
<%= f.input :car_id %>
<%= f.input :person_id %>
<%= f.input :car_id %>
<%= f.actions do %>
<%= f.action :submit, button_html: {class: "btn btn-primary"} %>
<% end %>
<% end %>
如您所见,输入字段是相同的。
我想对这些字段进行分组,以便它们可以像这样作为数组发布:
[ [3838, 9090], [2938, 893] ]
Ang 然后我可以循环这个数组并从外部获取我需要的数据 api。
我怎样才能做到这一点?
版本HTML
<input type="textbox" name="person_id[]", value="person A">
<input type="textbox" name="car_id[]", value="car A">
<input type="textbox" name="person_id[]", value="person B">
<input type="textbox" name="car_id[]", value="car B">
或
= simple_form_for @ some_model do |f|
= f.input_field :name, multiple: true
= f.input_field :name, multiple: true
= f.submit
你能不能在你的模型上创建另一个字段来保存数组。
然后创建一个私有控制器方法,该方法在保存表单时运行并将所有值附加到数组?
我有一个包含 4 个输入字段的表单。
<%= semantic_form_for @some_model, html: {class: "horizontal-form"} do |f| %>
<%= f.input :person_id %>
<%= f.input :car_id %>
<%= f.input :person_id %>
<%= f.input :car_id %>
<%= f.actions do %>
<%= f.action :submit, button_html: {class: "btn btn-primary"} %>
<% end %>
<% end %>
如您所见,输入字段是相同的。
我想对这些字段进行分组,以便它们可以像这样作为数组发布:
[ [3838, 9090], [2938, 893] ]
Ang 然后我可以循环这个数组并从外部获取我需要的数据 api。
我怎样才能做到这一点?
版本HTML
<input type="textbox" name="person_id[]", value="person A">
<input type="textbox" name="car_id[]", value="car A">
<input type="textbox" name="person_id[]", value="person B">
<input type="textbox" name="car_id[]", value="car B">
或
= simple_form_for @ some_model do |f|
= f.input_field :name, multiple: true
= f.input_field :name, multiple: true
= f.submit
你能不能在你的模型上创建另一个字段来保存数组。
然后创建一个私有控制器方法,该方法在保存表单时运行并将所有值附加到数组?