This is driving me crazy.
I have 2 models, StockShipment has_many Recipients.
It also has:
accepts_nested_attributes_for :recipients, reject_if: :all_blank, allow_destroy: true
I have this partial, that should create new rows for nested attributes with unique child_indexes
<%= form_with model: @stock_shipment do |form| %>
<%= form.fields_for(:recipients, Recipient.new, child_index: Time.now.to_i) do |recipient_form| %>
<%= turbo_stream.append('recipients-list', partial: 'shared/recipient_fields', locals: { form: recipient_form, new_record: true }) %>
<% end %>
<% end %>
It's creating new rows in the form, but every attribute is the same:
name="[recipients][phone]"
Instead of:
name="[recipients][12234324][phone]"
EDIT
In my Stock Shipment action, I'm passing the form object like this:
<tbody id="recipients-list">
<%= form.fields_for :recipients do |recipient_form| %>
<%= render 'shared/recipient_fields', form: recipient_form, locals: {user: current_user} %>
<% end %>
</tbody>
</table>
<%= link_to add_recipient_path(id: form.object),
class: 'inline-block inline-flex items-center mt-5 text-black',
id: 'add-new-recipient',
data: { turbo_stream: true } do %>
<span class="font-bold ml-3"><%= t('.add_recipient') %></span>
<% end %>
My routes
get :add_recipient, to: "stock_shipments#add_recipient"
** My controller **
def add_recipient end
This is making only 1 record to be created instead of various records separately.
Please, can someone help?
I'm using Rails 7.
Thank you.