【问题标题】:Submit multiple isolated forms in Rails在 Rails 中提交多个独立的表单
【发布时间】:2015-11-15 21:58:59
【问题描述】:

我正在寻找一种方法来提交使用 form_for 动态生成的多个表单,但到目前为止还没有成功。

这是一个活动的报名表,母亲可以在其中报名(使用一种表格)以及她的孩子(使用另一种不同的表格),如果她有的话。所以一个 user#show 页面可以有一个表单(对于母亲)或 4、5 等等。

这就是我所拥有的。当我使用remote: true 和每个提交按钮时,这些表单可以正常工作。但是带有一次性提交所有表单的脚本的单个按钮不起作用...

  <% if current_user == @user %>
    <% if @user.children %>
      <% @children.each do |child| %>
        <div class="attendance-form">
          <%= form_for(child, html: {id: "form-enfant-#{child.first_name}"}) do |f| %>
            <%= f.label child.first_name %>
            <%= f.check_box :attendance %>
          <% end %>
        </div>
      <% end %>
    <% end %>
    <%= form_for current_user, html: {id: "form-parent"} do |f| %>
      <%= f.label "Me" %>
      <%= f.check_box :attendance %>
    <% end %>

  <% end %>

  <div class="actions">
    <a id="submit">Submit</a>
  </div>



<script>
  $(function() {
    $('#submit').click(function() {
     <% @children.each do |child| %>
       <%= ("$('#form-enfant-" + child.first_name + "').submit();") %>
     <% end %>
      $('#form-parent').submit();
    });
  });
</script>

【问题讨论】:

    标签: jquery ruby-on-rails forms


    【解决方案1】:

    您不能提交多个表单,除非您使用 AJAX。所以要么让它们再次远程,要么编写一些代码来序列化表单数据并将其发布到服务器。

    而不仅仅是调用submit 调用这样的东西:

    $.post('/the/form/specific/url/', $('#form-x').serialize())
    

    【讨论】:

      【解决方案2】:

      我认为您需要的是 the concept of single table inheritance 和 the concept of nested forms 的组合。

      单表继承将使您拥有多种类型的用户:(母亲、父亲、儿子、女儿等)

      同时,

      嵌套表单将使您能够在表单中拥有关联的模型。 IE:母亲的表单,孩子的嵌套表单,一个提交按钮为您完成工作......

      我认为您应该能够将这两个概念结合起来,以便在您尝试做的事情上获得非常好的结果。

      祝你好运:-)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-11-22
        • 2018-09-15
        • 1970-01-01
        • 2011-03-20
        • 2016-04-06
        • 1970-01-01
        • 2021-02-03
        相关资源
        最近更新 更多