无法在单个视图上提交多个表单

时间:2014-12-28 22:19:53

标签: html ruby-on-rails forms simple-form nested-forms

我正在学习Rails制作一个(应该是)快速应用来展示一个想法。我已经摸索了这个视图代码。这是我的理解,因为我的提交按钮不在<form>标签内,我无法提交表格。我该如何解决这个问题?

<div class="PR">
  <%= simple_form_for @purchaserequest, html: {class: 'form-inline'}, defaults: { wrapper_html: { class: 'pr' }} do |f| %>
    <%= f.input :requestor %>
    <%= f.input :charge_number %>
    <%= f.input :date_of_intended_use %>
  <% end %>

  <%= simple_form_for @purchaserequest, html: {class: 'form-inline'}, defaults: { wrapper_html: { class: 'pr' }} do |f| %>
    <%= f.input :end_user %>
    <%= f.input :shipping_method %>
    <%= f.input :ship_to %>
  <% end %>
  <br><br>
</div>

<div class="pr_items_group">
<%= simple_form_for @purchaserequest, html: {class: 'form-inline items'}, defaults: { wrapper_html: { class: 'pr_items' }} do |pr_items| %>
  <%= pr_items.nested_fields_for :item, defaults: { wrapper_html: { class: 'pr_items' }} do |f| %>
    <%= f.input :critical %>
    <%= f.input :tech_reqs %>
    <%= f.input :description %>
    <%= f.input :manufacturer %>
    <%= f.input :item_number %>
    <%= f.input :purchased_quantity, label: 'Quantity', input_html: { class: 'quantity', onkeyup: 'updatePrice()' } %>
    <%= f.input :price, input_html: { class: 'price', onkeyup: 'updatePrice()' } %>
    <%= f.input :notes %>
    <%= f.remove_nested_fields_link 'Remove item', class: 'btn btn-primary' %>
    <br><br>
  <% end %>
  <%= simple_form_for @purchaserequest, html: {class: 'form-horizontal'}, defaults: { wrapper_html: { class: 'pr' }} do |f| %>
    <%= f.input :tax, :readonly => true  , input_html: { id: 'tax' } %>
    <%= f.input :shipping_total, input_html: { class: 'shipping_total', onkeyup: 'updatePrice()' } %>
    <%= f.input :grand_total, :readonly => true , input_html: { id: 'grand_total' } %>
  <% end %>
  <br>
  <p><%= pr_items.add_nested_fields_link :item, 'Add item',  class: 'btn btn-primary' %> | <%= pr_items.button :submit, 'Create Purchase Request', class: 'btn btn-primary actions' %></p>
<% end %>
</div>`

1 个答案:

答案 0 :(得分:1)

您需要一个simple_form_for方法,而不是多个方法。将所有字段和嵌套表单添加到传递给此方法的块中。

一个例子:

<%= simple_form_for @purchaserequest do |f| %>
  <%= f.input :requestor %>

  <%= f.input :ship_to %>

  <br><br>

  <%= f.nested_fields_for :item do |g| %>
    <%= g.input :critical %>

    <%= g.remove_nested_fields_link 'Remove item' %>

    <br><br>
  <% end %>

  <%= f.input :tax, readonly: true, input_html: { id: 'tax' } %>

  <%= f.submit %>
<% end %>