如何使用嵌套资源轨道从另一个模型显示模型字段5

时间:2017-09-02 05:35:39

标签: ruby-on-rails ruby ruby-on-rails-5 nested-resources

我有3个型号,第一个产品型号,发票和invoice_detail。

发票(父)和发票明细(子)是嵌套资源。

我在发票明细模型中添加产品,工作正常,问题是当我尝试在表格中显示添加的产品名称时,出现以下错误:

提前致谢!!

发票/ 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

1 个答案:

答案 0 :(得分:2)

在您看来,

final Stream< Integer > integerStream = Stream.of( modifiedNumber ).flatMap( n -> { final List< Integer > aList = new LinkedList<>(); int tempNumber, count = 0; while ( n > 0 ) { tempNumber = ( int ) ( ( n % 10 ) * Math.pow( 10, count ) ); aList.add( tempNumber ); n = n / 10; count++; } return aList.stream(); } );

应该是

<%= invoice_detail.product_id.name %>