Nested_form
Ryan Bates の #196 Railscasts Nested Forms 1に基づいてネストされたフォームを試してSimple_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"
初心者のための助けはありますか?ありがとうございました!!