0

I have the following three models and associations:

class ParentProduct < ApplicationRecord
  has_many :child_products
  accepts_nested_attributes_for :child_products, allow_destroy: true
  has_many :attachments, dependent: :destroy
  accepts_nested_attributes_for :attachments, allow_destroy: true
end

class ChildProduct < ApplicationRecord
  belongs_to :parent_product
  has_many :attachments, dependent: :destroy
  accepts_nested_attributes_for :attachments, allow_destroy: true
end

class Attachment < ApplicationRecord
  belongs_to :parent_product, optional: true
  belongs_to :child_product, optional: true
  mount_uploader :image, AttachmentUploader
end

The purpose of this setup is to create a parent product with its attachments. And if the user want's, he can create a child product along with its attachments and associate it to that parent product.

In the new parent_products_controller I have the following setup in order to initially add 8 attachment fields. Also I don't want to initially show the child_product_fields, that's why I haven't added this @parent_product.child_products.build:

def new
  @parent_product = ParentProduct.new
  8.times{ @parent_product.attachments.build }
end

Inside the form that I'm creating the parent product, I have the attachments nested fields in order to create the attachments for the parent product. And when the user clicks on the add a product variation link it opens up the child_product_fields:

<%= simple_form_for @parent_product, url: parent_product_path, method: :post, validate: true do |f| %>

  <%= f.simple_fields_for :attachments do |attachment| %>
    <%= render 'parent_products/attachment_fields', form: attachment  %>
  <% end %>

  <%= f.simple_fields_for :child_products do |child_product| %>
    <%= render 'parent_products/child_product_fields', form: child_product %>
  <% end %>

  <div class="links float-right" style="margin-bottom: 30px;">
     <%= link_to_add_association raw('<i class="far fa-plus-square"></i> Add a product variation'), f, :child_products, form_name: 'form', :style=>"color: grey; font-size: 14px;" %>
   </div>
<% end %>

Now inside the child_product_fields I have one more attachment nested fields in order to create the attachments for the child product.

<%= form.simple_fields_for :attachments do |attachment| %>
  <%= render 'parent_products/attachment_fields', form: attachment  %>
<% end %>

The issue now is that I cant get the 8 attachment fields to show when I click and open the child_product_fields. The setup I'm after is.... when I click and open the child_product_fields, the 8 attachment fields should also appear. Any idea on how I can implement this?

1 Answer 1

1

You can use the :wrap_object option of link_to_add_association. It allows to add extra initialisation before rendering the nested form. So in your case, you could add 8 attachments to a child-product.

E.g. do something like

<div class="links float-right" style="margin-bottom: 30px;">
  <%= link_to_add_association raw('<i class="far fa-plus-square"></i> Add a product variation'), 
        f, :child_products, form_name: 'form',
        wrap_object: Proc.new {|child| 8.times {child.attachments.build }; child }, 
        style: "color: grey; font-size: 14px;" %>
</div>
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the reply @nathanvda. I added the above code you've suggested, but only one attachments field appears, instead of 8.
Can you show us how you added this code? I do not understand how adding (building) 8 attachments, would then only render one.
I just replaced the entire <div class="links> I have, with your <div class="links> code. Thats all I did, it should work this way, right?
I got it to work with your code @nathanvda, I had a typo somewhere. Anyway, I'm noticing another issue, if the <%= f.simple_fields_for :child_products do |child_product| %> returns an error the 8 attachment fields disappear. How can I fix this?
I just tested it locally, and it should work. Glad it now works for you too. With regards to the error: without any further information, that is just plain rails/ruby/form behaviour, is it not? Please ask this in another question and provide some more information (what was the error? how did this manifest itself?). Please accept the answer if it helped you.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.