Reputation: 329
I have implemented the complex nested form based on Ryan Bates Screen cast.
I'm truing to have 2 nested forms in the same form
one is for Cutomer's contact and the other for customer's appointments
for that I have
patient model
class Patient < ActiveRecord::Base
has_many :contacts, :dependent => :destroy
accepts_nested_attributes_for :contacts, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
has_many :appointments, :dependent => :destroy
accepts_nested_attributes_for :appointments, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end
contact model
class Contact < ActiveRecord::Base
belongs_to :patient
end
Appointment model
class Appointment < ActiveRecord::Base
belongs_to :patient
end
appointment_fields partial form
<div class="fields">
Appointment: <%= f.datetime_select :appointment_date %>
<%= link_to_remove_fields "remove", f%><br />
</div>
contact_fields partial form
<div class="fields">
Contact: <%= f.text_field :contact_type %>
<%= f.text_field :content %>
<%= link_to_remove_fields "remove", f%><br />
</div>
patient _form partial
Edited to show only the formfields
<%= f.fields_for :contacts do |builder| %>
<%= render "contact_fields", :f => builder %>
<% end %>
<p><%= link_to_add_fields "Add More Contact", f, :contacts %></p>
<%= f.fields_for :appointments do |builder| %>
<%= render "appointment_fields", :f => builder %>
<% end %>
<p><%= link_to_add_fields "Add Appointment", f, :appointments %></p>
Application_helper
module ApplicationHelper
def link_to_remove_fields(name, f)
f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
end
def link_to_add_fields(name, f, association)
new_object = f.object.class.reflect_on_association(association).klass.new
fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
render(association.to_s.singularize + "_fields", :f => builder)
end
link_to_function(name, "add_fields(this, '#{association}', '#{escape_javascript(fields)}')")
end
end
Application.js
function remove_fields(link) {
$(link).prev("input[type=hidden]").val("1");
$(link).closest(".fields").hide();
}
function add_fields(link, association, content) {
var new_id = new Date().getTime();
var regexp = new RegExp("new_" + association, "g");
$(link).parent().before(content.replace(regexp, new_id));
}
I'm using rails 3.0.3 and ruby 1.9.2
The contac does work but the appointment does not. No error messages just does not run the include statement
here is the output when I enter the data in the form and enter submit
Started POST "/patients" for 127.0.0.1 at 2011-09-17 16:59:46 -0700
Processing by PatientsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"AEhUmtE5vIMrjHYWvGWRzlDc2hKrN0nc9gXPCSlIz50=", "patient"=>{"first_name"=>"test", "middle"=>"", "last_name"=>"", "dob(1i)"=>"2011", "dob(2i)"=>"9", "dob(3i)"=>"17", "address1"=>"", "address2"=>"", "city"=>"", "state"=>"", "country"=>"", "insurance_name"=>"", "contacts_attributes"=>{"0"=>{"contact_type"=>"cell", "content"=>"999-999-9999", "_destroy"=>"false"}, "1"=>{"contact_type"=>"", "content"=>"", "_destroy"=>"false"}}, "appointments_attributes"=>{"0"=>{"appointment_date(1i)"=>"2011", "appointment_date(2i)"=>"9", "appointment_date(3i)"=>"17", "appointment_date(4i)"=>"23", "appointment_date(5i)"=>"59", "_destroy"=>"false"}}, "notes"=>""}, "commit"=>"Create Patient"}
SQL (0.9ms) BEGIN
SQL (1.2ms) describe `patients`
AREL (1.5ms) INSERT INTO `patients` (`first_name`, `middle`, `last_name`, `dob`, `address1`, `address2`, `city`, `state`, `country`, `notes`, `insured`, `insurance_name`, `created_at`, `updated_at`) VALUES ('test', '', '', '2011-09-17 00:00:00', '', '', '', '', '', '', NULL, '', '2011-09-17 23:59:46', '2011-09-17 23:59:46')
SQL (3.2ms) describe `contacts`
AREL (0.3ms) INSERT INTO `contacts` (`patient_id`, `contact_type`, `content`, `created_at`, `updated_at`) VALUES (8, 'cell', '999-999-9999', '2011-09-17 23:59:46', '2011-09-17 23:59:46')
SQL (0.4ms) COMMIT
Redirected to http://localhost:3000/patients/8
Completed 302 Found in 74ms
If I do in the code below the console mode
@patients = Patient.find_by_id(1)
@patients.appointment
it returns [] which shows the relationship is right
Oh I tried to comment out contact but still was able to get it to work
Any ideas??
Upvotes: 1
Views: 1682
Reputation: 2132
In your patient model, in the appointment's nested_attributes_for declaration, you have a reject_if, for an non existent "content" attribute.
The content attribute is not a parameter of an appointment, it's only a parameter of a contact. There for, when you send the attributes, and your patient model sees that the appointment has a blank content attribute, it rejects the nested attributes for the appointment.
To fix it, remove the reject_if clause from the appointement nested_attributes_for declaration
accepts_nested_attributes_for :appointments, :allow_destroy => true
Or change it, for an existing attribute. like the appointement_date.
Let me know if this was the problem.
Upvotes: 1