Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple Nested Form
    primarykey
    data
    text
    <p>I have implemented the complex nested form based on Ryan Bates Screen cast.</p> <p>I'm truing to have 2 nested forms in the same form</p> <p>one is for Cutomer's contact and the other for customer's appointments</p> <p>for that I have </p> <p>patient model</p> <pre><code>class Patient &lt; ActiveRecord::Base has_many :contacts, :dependent =&gt; :destroy accepts_nested_attributes_for :contacts, :reject_if =&gt; lambda { |a| a[:content].blank? }, :allow_destroy =&gt; true has_many :appointments, :dependent =&gt; :destroy accepts_nested_attributes_for :appointments, :reject_if =&gt; lambda { |a| a[:content].blank? }, :allow_destroy =&gt; true end </code></pre> <p>contact model</p> <pre><code>class Contact &lt; ActiveRecord::Base belongs_to :patient end </code></pre> <p>Appointment model</p> <pre><code>class Appointment &lt; ActiveRecord::Base belongs_to :patient end </code></pre> <p>appointment_fields partial form</p> <pre><code>&lt;div class="fields"&gt; Appointment: &lt;%= f.datetime_select :appointment_date %&gt; &lt;%= link_to_remove_fields "remove", f%&gt;&lt;br /&gt; &lt;/div&gt; </code></pre> <p>contact_fields partial form</p> <pre><code>&lt;div class="fields"&gt; Contact: &lt;%= f.text_field :contact_type %&gt; &lt;%= f.text_field :content %&gt; &lt;%= link_to_remove_fields "remove", f%&gt;&lt;br /&gt; &lt;/div&gt; </code></pre> <p>patient _form partial</p> <pre><code>Edited to show only the formfields &lt;%= f.fields_for :contacts do |builder| %&gt; &lt;%= render "contact_fields", :f =&gt; builder %&gt; &lt;% end %&gt; &lt;p&gt;&lt;%= link_to_add_fields "Add More Contact", f, :contacts %&gt;&lt;/p&gt; &lt;%= f.fields_for :appointments do |builder| %&gt; &lt;%= render "appointment_fields", :f =&gt; builder %&gt; &lt;% end %&gt; &lt;p&gt;&lt;%= link_to_add_fields "Add Appointment", f, :appointments %&gt;&lt;/p&gt; </code></pre> <p>Application_helper</p> <pre><code>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 =&gt; "new_#{association}") do |builder| render(association.to_s.singularize + "_fields", :f =&gt; builder) end link_to_function(name, "add_fields(this, '#{association}', '#{escape_javascript(fields)}')") end end </code></pre> <p>Application.js</p> <pre><code>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)); } </code></pre> <p>I'm using rails 3.0.3 and ruby 1.9.2</p> <p>The contac does work but the appointment does not. No error messages just does not run the include statement</p> <p>here is the output when I enter the data in the form and enter submit</p> <pre><code>Started POST "/patients" for 127.0.0.1 at 2011-09-17 16:59:46 -0700 Processing by PatientsController#create as HTML Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"AEhUmtE5vIMrjHYWvGWRzlDc2hKrN0nc9gXPCSlIz50=", "patient"=&gt;{"first_name"=&gt;"test", "middle"=&gt;"", "last_name"=&gt;"", "dob(1i)"=&gt;"2011", "dob(2i)"=&gt;"9", "dob(3i)"=&gt;"17", "address1"=&gt;"", "address2"=&gt;"", "city"=&gt;"", "state"=&gt;"", "country"=&gt;"", "insurance_name"=&gt;"", "contacts_attributes"=&gt;{"0"=&gt;{"contact_type"=&gt;"cell", "content"=&gt;"999-999-9999", "_destroy"=&gt;"false"}, "1"=&gt;{"contact_type"=&gt;"", "content"=&gt;"", "_destroy"=&gt;"false"}}, "appointments_attributes"=&gt;{"0"=&gt;{"appointment_date(1i)"=&gt;"2011", "appointment_date(2i)"=&gt;"9", "appointment_date(3i)"=&gt;"17", "appointment_date(4i)"=&gt;"23", "appointment_date(5i)"=&gt;"59", "_destroy"=&gt;"false"}}, "notes"=&gt;""}, "commit"=&gt;"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 </code></pre> <p>If I do in the code below the console mode</p> <pre><code>@patients = Patient.find_by_id(1) @patients.appointment </code></pre> <p>it returns [] which shows the relationship is right</p> <p>Oh I tried to comment out contact but still was able to get it to work</p> <p>Any ideas??</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload