我正在尝试基于 Ryan Bates 的 #196 Railscasts Nested Forms 1 的嵌套表单。我正在使用Nested_form
和Simple_form
gems。也许代码正在与宝石竞争?
似乎不会消失的错误是
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"
对新手有帮助吗?谢谢!!