简体   繁体   English

如何在Rails中的两个表单中使用一个提交按钮?

[英]How can I use one submit button for two forms in rails?

I have a form that creates an object 'Visit' and a custom for stripe. 我有一个创建对象“访问”和自定义条纹的窗体。 I would like to merge the two forms and have only one submit button that processes both forms. 我想合并这两种形式,并且只有一个提交按钮可以处理两种形式。

Create Visit Form with stripe payment form rendered in it. 创建带有呈现在其中的条纹付款表格的访问表格。

<%= simple_form_for([@service, @visit]) do |f| %>

  <%= f.error_notification %>
  <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>
  <%= f.input :visit_date, as: :date , :label => "Service date:"%>
  <%= f.input :visit_time, as: :time , :label => "Service time:" %>
  <%= f.input :note, :label => "Anything we should know?" %>

  <%= render 'payments/payments-form' %>

  <%= f.button :submit, 'Schedule and Pay', class:'btn btn-bennett' %>

<% end %>

This is the stripe form. 这是条纹形式。

<%= form_tag payments_path, method: 'post', id: 'payment-form' do %>
  <span class="payment-errors"></span>

  <div class="form-row">
    <label>
      <span>Card Number</span>
      <input type="text" size="20" data-stripe="number"/>
    </label>
  </div>

  <div class="form-row">
    <label>
      <span>CVC</span>
      <input type="text" size="4" data-stripe="cvc"/>
    </label>
  </div>

  <div class="form-row">
    <label>
      <span>Expiration (MM/YYYY)</span>
      <input type="text" size="2" data-stripe="exp-month"/>
    </label>
    <span> / </span>
    <input type="text" size="4" data-stripe="exp-year"/>
  </div>

  <button type="submit">Submit Payment</button>
<% end %>

This is the show page where the form goes 这是表格所在的显示页面

<p>
  <strong>Name:</strong>
  <%= @service.name %>
</p>

<p>
  <strong>Price:</strong>
  <%= @service.price %>
</p>

<p>
  <strong>Estimated time:</strong>
  <%= @service.estimated_time %>
</p>

<p>
  <strong>Modal tag:</strong>
  <%= @service.modal_tag %>
</p>

<p>This is where the desciption of the service would go.
  This is where the desciption of the service would go.
  This is where the desciption of the service would go.
  This is where the desciption of the service would go.
  This is where the desciption of the service would go.
</p>

<h1>Set Service: </h1>

<%= render 'visits/form' %>

<%= link_to 'Edit', edit_service_path(@service) %> |
<%= link_to 'Back', services_path %>

</div>

Currently, there are two submit buttons. 当前,有两个提交按钮。 I would like there to be only one. 我希望只有一个。 How do I do this? 我该怎么做呢? Thank you! 谢谢!

  1. Unify Controller 统一控制器

In order to make 2 forms to be processed simultaneously, you would need to make the controller to receive params from both the forms and process accordingly. 为了使2个表单同时处理,您需要使控制器从两个表单中接收参数并相应地进行处理。 In this case, you would need to do proper rollbacks in case of failure to one of the operations. 在这种情况下,如果其中一项操作失败,则需要进行适当的回滚。 You may look at gems like Interactor if in case your use case becomes more complex. 如果您的用例变得更加复杂,则可以看一下Interactor之类的瑰宝。

  1. Independent AJAX requests 独立的AJAX请求

If two of the requests are independent, you can make them into ajax calls (asynchronous); 如果两个请求是独立的,则可以使它们成为ajax调用(异步)。 and just have one button to post one form after another. 并且只有一个按钮可以发布一种表格。 Example code: 示例代码:

$('.single-submit-btn').click(function(){
  $('#form1').submit();
});

$("#form1").bind('ajax:complete', function() {
  $('#form2').submit();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM