如何使用一个用于两种形式提交在轨按钮?

问题描述 投票:0回答:1

我有一个创建一个对象“访问”和条纹的自定义表单。我想合并两种形式,并只有一个提交按钮,处理这两种形式。

在它呈现条纹付款形式创建访问表。

<%= 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 %>

这是条纹状。

<%= 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 %>

这是展示页面,形式变

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

目前,有两个提交按钮。我想那里只有一个。我该怎么做呢?谢谢!

ruby-on-rails ruby ruby-on-rails-5
1个回答
0
投票
  1. UNIFI控制器

在同时处理,以使2种形式,你需要使控制器从两个相应的表单和流程得到PARAMS。在这种情况下,你需要在失败的情况下做适当的回滚到操作之一。你可以看看像Interactor宝石如果万一你的使用情况变得更加复杂。

  1. 独立AJAX请求

如果两个请求都是独立的,你可以让他们到Ajax调用(异步); ,只是有一个按钮陆续发布一种形式。示例代码:

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

$("#form1").bind('ajax:complete', function() {
  $('#form2').submit();
});
© www.soinside.com 2019 - 2024. All rights reserved.