title: Automatically adding nested child partials on link_to_add_association click using Cocoon
body: <p>I have a form with 4 nested levels that I'm using to dynamically add items, shipments, and calculate costs. I have the form set up to where I can add/remove everything I need with button clicks. Everything about the initial render of the page is as expected.</p>
  <p>However, when I go to add new instances of the mid-level nests (the SubQuote and QuoteShipment), it only renders itself (e.g. SubQuote) and not any of the partials referenced in its partial (QuoteShipment).  I want a button click to render all of the nested partials beneath of it automatically.</p>
  <p>My relevant models are structured:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>class Quote &lt; ApplicationRecord
    has_many :sub_quotes, dependent: :destroy, inverse_of: :quote
    accepts_nested_attributes_for :sub_quotes, reject_if: :all_blank
  end
  
  
  class SubQuote &lt; ApplicationRecord
      belongs_to :quote
      has_many :quote_shipments, dependent: :destroy, inverse_of: :sub_quote
      accepts_nested_attributes_for :quote_shipments, reject_if: :all_blank
  end
  
  class QuoteShipment &lt; ApplicationRecord
      has_many :quote_items, dependent: :destroy, inverse_of: :quote_shipment, class_name: "::QuoteItem"
      belongs_to :sub_quote
      accepts_nested_attributes_for :quote_items, reject_if: :all_blank
  end
  
  Class QuoteItem &lt; ApplicationRecord
      belongs_to :quote_shipment
  end</code></pre>
  </div>
  </div>
  </p>
  <p>Partial for SubQuote class that renders the QuoteShipment partial on page load, but not on link_to_add_association click.</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div class="col nested-sub_quote-fields" id="sub_quote_identifier"&gt;
  
    &lt;div class="row"&gt;
      &lt;%= link_to_remove_association button_tag('remove sub_quote', type: "button", class: "btn btn-sm btn-outline red-btn add-quote"), f , { wrapper_class: "nested-sub_quote-fields" }%&gt;
    &lt;/div&gt;
  
    &lt;div class="row" class=""&gt;
    
      &lt;div id="sub_quote" class="col mt-0 mt-md-4 mb-4 p-0"&gt;
        &lt;div class="col "&gt;
          &lt;div class="col-lg col-sm-12 col-md-12"&gt;
            &lt;%= f.fields_for :quote_shipments do |shipment| %&gt;
              &lt;%= render partial: "quotes/quote_shipment_fields", :locals =&gt; { :f =&gt; shipment } %&gt;
            &lt;% end %&gt;
            &lt;div class='links'&gt;
              &lt;%= link_to_add_association button_tag('Add Shipment', type: "button", class: "btn btn-sm btn-outline btn-primary add-quote"), f, :quote_shipments %&gt;
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      
      &lt;div class="col-lg-5 col-sm-12 col-md-12"&gt;
        &lt;%= render partial: "quotes/cost", locals: { f: f } %&gt;
      &lt;/div&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  <p>For example with the above relationships:</p>
  <ul>
  <li><p>If I clicked the link_to_add_association for the SubQuote class, I would like for it to render the SubQuote partial, as well as 1 instance of the QuoteShipment partial, and 1 instance of the QuoteItem partial.</p>
  </li>
  <li><p>If I clicked the link_to_add_association for the QuoteShipment partial, I'd like it to render with 1 instance of the QuoteItem partial nested beneath the new QuoteShipment partial.</p>
  </li>
  </ul>
  <p>I've tried:</p>
  <ul>
  <li>Using the &quot;insert-after&quot; method in Jquery, however the partials need the fields_for variable which I don't believe is accessible</li>
  <li>Instantiating more instances on the Controller</li>
  <li>Alot of Googling. Either no one is asking the question, or I'm searching it the wrong way</li>
  </ul>
  <p>I think in a worse case scenario, I could generate different partials for each different level's link_to_add_association button that I have, but that seems very redundant</p>