Rails 4具有多个标记字段的嵌套表单

时间:2014-11-13 16:34:56

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

我有一个rails 4应用程序,在cash_receipt和cash_receipt_lines之间有一个简单的has_many和belongs_to关系。 cash_receipt_lines作为嵌套表单输入。我的问题来自提交嵌套字段的正确方法。用户将输入基本cash_receipt数据,选择客户并输入支票号和金额。这将打开一个包含check_box_tag和金额行的未结发票列表,作为text_field_tag。

cash_receipt_form

<%= simple_form_for @cash_receipt, wrapper: :bootstrap, html: { class: 'form-horizontal' } do |f| %>
  <%= f.error_notification %>

  <div class="well">
    <div class="span3"> 
    <%= f.input :account_id, label: false, :collection => GlAccount.all, :value_method => :id, :label_method => 
          :fullaccount, :required => false, input_html: { id: "account-lookup", class: "input-xlarge" }  %>  
</div>
<div class="span5">      
<%= f.input :client_id, label: false, collection: Client.all, value_method: :id, label_method: :name,
           input_html: { id: "client-lookup", class: "input-xlarge" } %>           
</div>
<div class="span2">
<%= f.input :no_ar, as: :boolean, checked_value: true, unchecked_value: false, label: "Ledger Account?" %>   
</div>
  </div>

   <div class="well">
  <div class="row">
    <div class="span4"> 
  <%= f.input :payment_type, collection: ['Check', 'Cash'], input_html: { :class => "span2" } %>
<%= f.input :check_number, :as => :string, label: "Check #", input_html: { :class => "span2" } %>
 </div>
<div class="span5"> 
 <%= f.input :payment_date, :as => :string, :label => "Payment Date", required: false, :input_html => { :class => "datepicker span2", value: Time.now.strftime('%m-%d-%Y') } %>
<%= f.input :amount, label: "Payment Amount", required: false, :as => :currency, input_html: { :class => "span2", 
  id: "client-amount" } %>
</div>
</div>
</div>  

<div class="well">
<div class="row">     
 <div class="span1">
  <h6>Open Items</h6>
    <p></p>
 </div>
</div>

    <div id="openitems">

    <%= render :partial => "payment_lines", :locals => { :aropen => @aropen } unless @aropen.nil? || @aropen.empty? %>
  </div>
</div>          

  <div class="form-actions">
    <%= f.submit "Save Cash Receipt", :class => "btn btn-primary" %>
    <%= f.button :cancel, to: cash_receipts_path %>
   </div><!-- actions -->


<script type="text/javascript">
 $(document).ready(function() {
    $('select#client-lookup').select2({
       placeholder: "Choose a Client",
  allowClear: true
});
$('select#account-lookup').select2({
  placeholder: "Deposit To Account",
  allowClear: true
});
$('select#client-lookup').bind('change', function() {
  jQuery.ajax({
    url: "<%= update_aropen_cash_receipts_path %>",
    data: {
      client_id : $('select#client-lookup').val()
    },
    dataType: "script"
  });
});  
});
</script>
 <%end%>

并且cash_receipt_lines部分

<table class ="table table-striped">
  <thead>
    <tr>
     <th>Selected</th>
     <th>Invoice #</th>
     <th>Balance</th>
     <th>Invoice Amount</th>
     <th>Client</th>
     <th>Invoice Date</th>
     <th>Amount to Apply</th>
    </tr>
  </thead>
  <tbody>
  <% aropen.each do |ar| %>
    <tr>
      <td><%= check_box_tag "invoice_header_id[]", ar.id %></td>
      <td><%= ar.id%></td>
      <td><%= number_to_currency(ar.balance) %></td>
      <td><%= number_to_currency(ar.total_amount) %></td>
      <td><%= ar.name %></td>
      <td><%= ar.invoice_date.strftime("%m-%d-%Y") %></td>
      <td><%= text_field_tag "cash_receipt[cash_receipt_lines][][applied]" %></td>
    </tr>
  <% end %>
  </tbody>
</table>

因此,partial是一个包含未结发票清单的简单表格,它们会检查多个并输入金额。在提交时,我需要能够与父级一起创建每个子记录。

型号:

class CashReceipt < ActiveRecord::Base
  has_many :cash_receipt_lines, dependent: :destroy
  belongs_to :invoice_header
  accepts_nested_attributes_for :cash_receipt_lines, reject_if: :all_blank, allow_destroy: true
end 

class CashReceiptLine < ActiveRecord::Base
  belongs_to :cash_receipt

end

我开始使用cocoon,但这不适合发票清单。参数仍然设置在控制器中:

 def cash_receipt_params
  params.require(:cash_receipt).permit(:payment_date, :payment_type, :account_id, :client_id, :check_aba, :check_number,
                                     :amount, :ar_account_id, :no_ar,:posted, :user_id, 
                                   cash_receipt_lines_attributes: [ :id, :cash_receipt_id, :applied_amount, :invoice_header_id, 
                                   :updated_at, :created_at, :unappliedar, :_destroy])
end 

如何为每个子记录使用多个标记字段时,为create方法正确设置params并保存父级和子级?

1 个答案:

答案 0 :(得分:0)

我终于弄明白了。通过更改cash_receipt_lines部分中的数组:

变化:

<td><%= check_box_tag "invoice_header_id[]", ar.id %></td>

<td><%= text_field_tag "cash_receipt[cash_receipt_lines][][applied]" %></td>

要:

<td><%= check_box_tag "cash_receipt[cash_receipt_lines_attributes][#{ar.id}][invoice_header_id]", ar.id %></td>

<td><%= text_field_tag "cash_receipt[cash_receipt_lines_attributes][#{ar.id}][applied_amount]","" %></td>

修复了这个问题。没有其他变化,它会正确创建正确的参数和帖子!关键是为数组添加索引号。只是以为我会回答客栈的情况,其他人就像我一样把头发拉出来!