0

我有一个发票控制器和两个模型发票和 invoice_line_items 并使用 invoice_line_item 类创建 tax_line_items。下面是我的代码:

这是我的控制器代码:

class InvoicesController < ApplicationController
def new
    @menu = 'Income'
    @page_name = 'Create new invoice'
    @invoice = Invoice.new
    @invoice.invoice_line_items.build
    @invoice.tax_line_items.build

    respond_to do |format|
      format.html
      format.xml { render :xml => @invoice }
    end
  end
end

我的发票模型:

class Invoice < ActiveRecord::Base

  has_many :invoice_line_items
  has_many :tax_line_items, :class_name => "InvoiceLineItem",:dependent => :destroy

  accepts_nested_attributes_for :invoice_line_items, :reject_if => lambda {|a| a[:account_id].blank? } , :allow_destroy => true
  accepts_nested_attributes_for :tax_line_items, :reject_if => lambda {|a| a[:account_id].blank? } , :allow_destroy => true
end

在我的表格中,我使用了:

<%= form_for(@invoice) do |f| %>
<%= render 'shared/form_error', :object => @invoice %>
<% @invoice.invoice_line_items.each_with_index do |invoice_line_item, index| %>
                   <%= render "invoice_line_items", :invoice_line_item => invoice_line_item, :index => index %>
                <% end %>
<% @invoice.tax_line_items.each_with_index do |tax_line_item, tax_index| %>
                      <%= render "tax_line_items", :tax_line_item => tax_line_item, :tax_index => tax_index %>     
                <% end %>
<%end%>

部分:

1) invoice_line_items:

<tr id="row<%= index %>" valign="top" >
            <%= hidden_field_tag "invoice[invoice_line_items_attributes][#{index}][id]",invoice_line_item.id%>
            <td valign="top">
                <%= select_tag "invoice[invoice_line_items_attributes][#{index}][account_id]", options_from_collection_for_select(@from_accounts, :id, :name,:selected => invoice_line_item.account_id ), :include_blank => true, :class=>"full"  %>
              <!-- <a href="/accounts/new?account_head_id=10" > New item</a> -->
            </td>
      <td><%= text_area_tag "invoice[invoice_line_items_attributes][#{index}][description]",invoice_line_item.description, :class => "full", :cols => 10, :rows=>1 %></td>
      <td><%= text_field_tag "invoice[invoice_line_items_attributes][#{index}][quantity]",invoice_line_item.quantity, :class => 'full', :id => 'quantity', :onkeydown => "return numbersOnly(event);", :size => 5, :maxlength => 25 %></td>
      <td><%= text_field_tag "invoice[invoice_line_items_attributes][#{index}][unit_rate]",invoice_line_item.unit_rate, :class => 'full', :id => 'unit_cost', :onkeydown => "return numbersOnly(event);", :size => 5, :maxlength => 20 %></td><!--Jquery code is in application.js-->
      <td><%= text_field_tag "invoice[invoice_line_items_attributes][#{index}][discount_percent]", invoice_line_item.discount_percent, :class => 'full', :id => 'discount', :onkeydown => "return numbersOnly(event);", :maxlength => 5, :size => 5%></td>
      <td><%= text_field_tag "invoice[invoice_line_items_attributes][#{index}][amount]", invoice_line_item.amount, :class => 'full', :id => 'total', :readonly => 'readonly',  :size => 5%></td>
            <td><%= link_to image_tag("/images/black_icon/ic_cancel.png"),{:action => :remove_line_item, :index => index}, :remote => true unless index == 0 %></td>
    </tr>

和 2) tax_line_items:

<tr id="tax_row<%= tax_index %>" valign="top" >
   <%= hidden_field_tag "invoice[tax_line_items_attributes][#{tax_index}][id]",tax_line_item.id%>
   <%= hidden_field_tag "invoice[tax_line_items_attributes][#{tax_index}][tax]", tax_line_item.tax, :value => 1 %>
   <td style="background:#EDF4FF"></td>
   <td  class="ta-right" style="background:#EDF4FF" colspan="2"><label>Add Tax:</label></td>
   <td class="ta-right"  colspan="2" style="background:#EDF4FF">
      <%= select_tag "invoice[tax_line_items_attributes][#{tax_index}][account_id]", options_from_collection_for_select(@tax_accounts, :id, :name,:selected => tax_line_item.account_id ), :include_blank => true, :class=>"full"  %>
      <!-- <a href="/accounts/new?account_head_id=10" > New item</a> -->
   </td>
   <td style="background:#EDF4FF"><%= text_field_tag "invoice[tax_line_items_attributes][#{tax_index}][amount]", tax_line_item.amount, :class => 'full', :id => 'tax', :onkeydown => "return numbersOnly(event);", :size => 5%></td>
   <td style="background:#EDF4FF"><%= link_to image_tag("/images/black_icon/ic_cancel.png"),{:action => :remove_tax_item, :tax_index => tax_index}, :remote => true %></td>

</tr>

我可以轻松添加新的行项目条目,并在它们不是税收项目时对其进行编辑。问题是,当它们是在任何条目中创建的税收项目,然后尝试编辑此条目时,我的行项目行会出现多次,例如假设我有一个税收项目,它们会为发票行项目显示一个新行和一个额外的行对于税单项目。我认为这会发生,因为我为这两个行项目使用了一个公用表。我这样做是因为我必须将两个行项目条目保存在同一个表中,因为两个帐户都来自同一个表。如果有人有正确的答案,我非常感谢。非常感谢

4

1 回答 1

1

正如这个答案所暗示的,您需要在此表中添加一个“类型”列以区分发票和税项。它将从您的代码中消除很多复杂性,因为 Rails 将完成从公共表中识别哪个记录属于哪个模型类的工作。

于 2012-05-17T06:19:31.797 回答