nathanvda / cocoon

Dynamic nested forms using jQuery made easy; works with formtastic, simple_form or default forms
http://github.com/nathanvda/cocoon
MIT License
3.08k stars 383 forks source link

Not rendering form field in the viewthat is nested 2 step deep in the form. #627

Closed JuzerShakir closed 1 year ago

JuzerShakir commented 1 year ago

I have 3 models, Products, Property & ProductProperty. Here's how they relate : The Product model:

  has_many :product_properties
  accepts_nested_attributes_for :product_properties

  has_many :properties, through: :product_properties

The Property Model:

    has_many :product_properties
    has_many :products, through: :product_properties

And the ProductProperty Model:

  belongs_to :product
  belongs_to :property
  accepts_nested_attributes_for :property

The Product form:

<%= form_with model: @product do | prod | %>
    <%= prod.label :name %>
    <%= prod.text_field :name %>

    <br>

    <%= prod.label :upc, "UPC" %>
    <%= prod.text_field :upc %>

    <br>

    <%= prod.label :available_on %>
    <%= prod.date_field :available_on %>

    <br>

    <h3>Properties</h3>

    <table>
        <thead>
        <tr>
            <th>Property Value</th>
            <th>Property Name</th>
        </tr>
        </thead>
        <tbody class="product_properties">
            <%= prod.fields_for :product_properties do | prod_prop | %>
                <%= render "product_property_fields", f: prod_prop %>
            <% end %>
        </tbody>
    </table>

    <br>

    <%# <%= link_to_add_fields('Add More Properties', prod, :product_properties) %>
    <div class="links">
        <%= link_to_add_association 'Add More Properties', prod, :product_properties %>
    </div>

    <br>

    <%= prod.submit %>
<% end %>

Inside the product_property_fields partial:

<tr>
    <td><%= f.text_field :value %></td>
    <%= f.fields_for :property do | prop | %>
        <td><%= prop.text_field :name %></td>
    <% end %>
</tr>

So I have a Product Model that has nested attributes for Product_Property Model and it has nested attributes for Property Model. And I want to dynamically create attributes of both ProductProperty & Property Model. In my view when I click on the "Add more attributes" link it only creates new fields for the product_properties form and not for the property form which is nested inside the product_properties form.

So how do I create the property form and the product_properties form together with a click?

JuzerShakir commented 1 year ago

.

JuzerShakir commented 1 year ago

I had to add an option in 'link_to_add_association' which fixed the issue