【问题标题】:How to post grouped input data within a form in Rails如何在 Rails 的表单中发布分组的输入数据
【发布时间】:2020-01-29 16:11:12
【问题描述】:

我有一个包含 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] ]

然后我可以循环这个数组并从外部 api 获取我需要的数据。

我怎样才能做到这一点?

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4


    【解决方案1】:

    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
    

    【讨论】:

      【解决方案2】:

      你能不能在你的模型上创建另一个字段来保存数组。

      然后创建一个 Private 控制器方法,该方法在保存表单时运行并将所有值附加到数组中?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-12-02
        • 2017-10-30
        • 1970-01-01
        • 1970-01-01
        • 2012-04-17
        • 2015-10-31
        相关资源
        最近更新 更多