2

I'm attempting the nested forms based on Ryan Bates' #196 Railscasts Nested Forms 1. I'm using Nested_form and Simple_form gems. Maybe code is competing with gems?

The error that won't seem to go away is

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

This occurs when I'm on the Survey page and submitting "New Survey".

I've got a Customer model and a Contact model which are associated to Survey.

It's referring to the last line in Survey Controller:

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

Solutions I tried include doing migrations adding the CustomerId to the Survey table or the Customer table. I've currently removed all references to customer_id.

Here's 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

Here's 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

here is contact.rb

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

and here's my 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"

Any help for a newbie? Thank you!!

PatrickLightning
  • 117
  • 1
  • 12

0 Answers0