【问题标题】:Rails 4 Nested Form with Multiple Tag Fields带有多个标签字段的 Rails 4 嵌套表单
【发布时间】:2014-11-13 16:34:55
【问题描述】:

我有一个 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>

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

型号:

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 方法的参数并在每个子记录使用多个标签字段时保存父子记录?

【问题讨论】:

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


    【解决方案1】:

    我终于想通了。通过更改 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>
    

    解决了这个问题。没有其他更改,它会创建正确的参数并正确发布!关键是向数组添加索引号。只是想我会回答旅馆,以防其他人像我一样拔头发!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-28
      • 2016-02-03
      • 1970-01-01
      • 2014-08-02
      • 1970-01-01
      相关资源
      最近更新 更多