Rails 3中未知属性Id上的嵌套表单错误

时间:2013-06-12 19:09:27

标签: ruby-on-rails associations nested-forms railscasts

我正在尝试基于Ryan Bates的#196 Railscasts嵌套表单的嵌套表单1.我正在使用Nested_formSimple_form宝石。也许代码与宝石竞争?

似乎不会消失的错误是

ActiveRecord::UnknownAttributeError at   /surveys/new
unknown attribute: customer_id

当我在Survey页面上并提交"New Survey"时会发生这种情况。

我有一个与Customer相关联的Contact模型和Survey模型。

它指的是Survey Controller中的最后一行:

def new
   12     @survey = Survey.new
   13     3.times do
   14       customer = @survey.customers.build
   15       2.times { customer.contacts.build }

我尝试过的解决方案包括进行迁移,将CustomerId添加到Survey表或Customer表。我目前删除了对customer_id的所有引用。

这是survey.rb

class Survey < ActiveRecord::Base
  has_many :contacts
  has_many :customers, :dependent => :destroy
  accepts_nested_attributes_for :customers, :reject_if => lambda { |a| a[:content].blank? } :allow_destroy => true
  attr_accessible :name, :customers_attributes
end

这是customer.rb

class Customer < ActiveRecord::Base
  belongs_to :survey
  has_many :contacts
  attr_accessible :company, :first_name, :last_name, :contacts_attributes, :customer_id
  accepts_nested_attributes_for :contacts, :reject_if => lambda { |a| a[:content].blank? } :allow_destroy => true
end

这里是contact.rb

class Contact < ActiveRecord::Base
  attr_accessible :mobile_phone, :email
  belongs_to :customer
end

这是我的form.html.haml

= simple_nested_form_for @survey do |f|
  = f.error_notification
  = f.input :name
  = f.fields_for :customers do |builder|
    = render "customer_fields", :f => builder
    %p= f.submit "Submit"

对新手有什么帮助吗?谢谢!!

0 个答案:

没有答案