0

我有一个包含 many_invoice 项目的发票模型

class Invoice < ActiveRecord::Base
  belongs_to :customer, :inverse_of => :invoices
  attr_accessible :approved_by, :due_date, :invoice_date, :reading_ids, :terms, :customer_id, :customer, :status

  validates :invoice_date, presence: true
  validates :due_date, presence: true
  validates :customer, presence: true
  has_many :invoice_items
  accepts_nested_attributes_for :invoice_items
end

发票项目模型

class InvoiceItem < ActiveRecord::Base
  belongs_to :invoice
  attr_accessible :amount, :description, :rate, :tax_amount
end

我现在在我的 Invoices_controller 中有一个显示操作

def show
@invoice = Invoice.find(params[:id])
respond_to do |format|
    format.html
end
end

我希望能够在发票的显示页面中显示发票项目,如描述、税额和税率,但是,这给了我很大的挑战。我是否必须在其中创建一个处理发票项目的部分?下面是我的展示页面

<p id="notice"><%= notice %></p>
<div class="row">
<div class="span12">
    <h3> Customer Invoices </h3>
<table class="table table-striped">
  <thead>
    <tr>
      <th>Invoice ID </th>
      <th>Customer Name </th>
      <th>Invoice Date </th>
      <th>Due Date </th>
      <th>Amount</th>     
   </tr>
</thead>
<tbody>
  <tr>
    <td><%= @invoice.customer.name %></td>
    <td><%= @invoice.invoice_date %></td>
    <td><%= @invoice.due_date %></td>   
  </tr>
</tbody>
</table>
</div>
</div>
4

2 回答 2

1

使用部分不是必须的,但你可以通过任何一种方式来做到这一点

1 - 没有部分

in your show.html.erb

#your invoice code
<% invoice_items = @invoice.invoice_items %>
<% invoice_items.each to |invoice_item|%>
<tr>
  <td><%= invoice_item.amount%></td>
</tr>
<% end %>

2)带偏

in your show.html.erb

#your invoice code
    <% invoice_items = @invoice.invoice_items %>
    <% invoice_items.each to |invoice_item|%>
    <tr>
      <td>
         <%= render :partial => 'invoice_item', :locals => {:item => invoice_item}%>
      </td>
    </tr>
    <% end %>

 in your _invoice_item.html.erb

 <%= item.name %>

高温高压

于 2012-12-31T10:45:40.977 回答
0

您可以使用部分来保持整洁,但没有理由不能在显示视图模板中执行此操作。

<% @invoice.invoice_items.each do |item| %>
 <td><%= item.amount %></td>
 <td><%= item.description %></td>
 # etc
<% end %>

发票项目与@invoice您在视图中拥有的对象相关,因此您可以访问发票invoice_items

于 2012-12-31T10:41:14.093 回答