1

拥有三种型号:

class Offer < ActiveRecord::Base
   has_many :offer_items
end

class OfferItem < ActiveRecord::Base
  belongs_to :offer
  belongs_to :partner_product
  accepts_nested_attributes_for :partner_product
end

class PartnerProduct < ActiveRecord::Base
 has_many :offer_items
 accepts_nested_attributes_for :offer_items
end

在编辑offer的表单中,有fields_for可以编辑offer_item

<% f.fields_for :offer_items do |offer_item_form| -%>
 <div class='offer_item'>
   <%= render :partial => 'offer_item_fields', :locals => {:offer_item_form => offer_item_form}%>
      </div>
<% end -%>

部分代码块:

<%= offer_item_form.object.partner_product.name%>

抛出错误

nil:NilClass 的未定义方法“名称”

提取的源代码(第 29 行附近):

<%= offer_item_form.object.partner_product.name%>

但是当我不问名字时,就像这样

<%= offer_item_form.object.partner_product%>
<%= debug( offer_item_form.object.partner_product)%>

进入浏览器:

#<PartnerProduct:0x007fa8785ff938>

--- !ruby/ActiveRecord:PartnerProduct 
attributes: 
id: 1
partner_id: 586
name: a
stock_volume: 0
source_price: 0
created_at: 2012-05-31 14:05:47 Z
updated_at: 2012-05-31 14:05:47 Z

我不只是想知道,为什么我无法访问“名称”属性并将 partner_product 更改为 nil,但我真的应该展示它。欢迎任何帮助!谢谢

4

0 回答 0