2017-09-02 34 views
0

我有3個模型,第一個產品模型,發票和invoice_detail。如何使用嵌套資源導航欄顯示來自其他模型的模型字段5

發票(父)和發票明細(子)是嵌套資源。

上午將產品從發票明細模式,即工作正常,問題是當我試圖在表中顯示添加的產品的名稱,我得到了如下錯誤:

提前感謝! !

發票/ show.html.erb

undefined method `name' for 2:Integer 

發票/ show.html.erb

<% @invoice.invoice_details.each do |invoice_detail| %> 
     <tr> 
      <td><%= invoice_detail.product_id.name %></td> 
      <td><%= number_to_currency invoice_detail.product_id.price %></td> 
      <td><%= invoice_detail.product_id.quantity %></td> 
      <td><%= number_to_currency invoice_detail.total_amt %></td> 

      <td> 


       <%= link_to "Destroy", 
            [@invoice, invoice_detail], 
            method: :delete, 
            remote: true, 
            data: { confirm: "Are you sure?" }, 
            class: "btn btn-default"%> 
      </td> 
     </tr> 
     <% end %> 

型號/ product.erb

class Product < ApplicationRecord 
    belongs_to :category 
    belongs_to :brand 
    belongs_to :unit 
    belongs_to :warehouse 
    belongs_to :user 
    has_many :invoice_details 
end 

型號/ invoice.erb

class Invoice < ApplicationRecord 
    belongs_to :warehouse 
    belongs_to :invoice_type 
    belongs_to :user 
    belongs_to :customer 
    belongs_to :provider 
    has_many :invoice_details, :dependent => :destroy 
end 

型號/ invoice_detail.erb

class InvoiceDetail < ApplicationRecord 
    belongs_to :invoice 
    belongs_to :product 
    belongs_to :tax 
end 

回答

2

在您看來,

<%= invoice_detail.product_id.name %>

應該

<%= invoice_detail.product.name %>

+0

它不工作我的朋友。現在我正在爲nil:NilClass ** @Andy獲取** undefined方法'name' –

相關問題