Hello,
I am trying to build a remote form_for form for my customer model that has a nested field for my phone number model. I do not know ahead of time how many phone numbers will be submitted for each customer. No matter what I do, I can't get this form to submit correctly or pass the right params.
Here's what I have right now (view is HAML) in estimates/new:
= form_for(Customer.new, :remote => true) do |customer_form|
= customer_form.text_field :last_name
= customer_form.text_field :first_name
= customer_form.fields_for :phone_numbers do |phone_form|
= phone_form.text_field :number
Customers controller:
class CustomersController < ApplicationController
def new
@customer = Customer.new
end
def create
@customer = Customer.new(params[:customer])
if @customer.save
session[:customer_id] = @customer.id
else
flash[:error] = "Estimate could not be saved!"
end
end
end
PhoneNumbers controller:
class PhoneNumbersController < ApplicationController
def create
@phone_number = PhoneNumber.new(params[:phone_number])
@phone_number.save
render :nothing => true
end
end
Customer model:
class Customer < ActiveRecord::Base
has_many :estimates
has_many :phone_numbers
accepts_nested_attributes_for :phone_numbers
end
PhoneNumber model:
class PhoneNumber < ActiveRecord::Base
belongs_to :customer
end
So, my question is: Can I have a nested form inside a remote form with Rails 3? How would I even do this? Has the syntax for this changed in Rails 3?
Thanks.