当我尝试访问属性时,rails fields_for对象为nil

时间:2012-06-05 16:14:36

标签: ruby-on-rails activerecord fields-for

有三种型号:

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_item的fields_for

<% 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的未定义方法`name':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

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

0 个答案:

没有答案