user3074558
user3074558

Reputation: 133

Nested forms not appending to params nested attributes (COCOON RAILS)

I'm making a nested form with Cocoon gem for my Campaign model, in order to add items by using link_to_add_association to add new item rows. My associations are working correctly.

I can see in my logs the items_attributes are not being appended. I can't find what I'm missing...

  Campaign Create (1.8ms)  INSERT INTO "campaigns" ("title", "user_id", "status", "created_at", "updated_at", "numberofitems", "shipping_type") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["title", "asdfasdf"], ["user_id", 52], ["status", 1], ["created_at", "2022-11-07 18:25:31.782846"], ["updated_at", "2022-11-07 18:25:31.782846"], ["numberofitems", 11], ["shipping_type", 1]]

Form

<%= form_with(model: [@user, @campaign], url: admin_create_campaign_path, method: :post, local: true) do |f| %>
  <%= render partial: 'step_1', locals: {f: f} %>
  <h1>Añadir items a la campaña</h1>
      <%= f.fields_for :items do |ff| %>
        <%= render partial: 'item_fields',  ff: ff %>
      <% end %>
      <%= link_to_add_association 'Añadir item', f, :items %>
    <%= f.submit "Crear campaña", class: "btn btn-primary" %>
<% end %>

item_fields partial:

<div class="nested-fields">
  <div class="card">
      <%= f.hidden_field :_destroy %>
    <div class="field form-group">
      <%= f.label :product_id, "Producto asociado al item", class: "form-label" %>
      <%= f.select :product_id, options_from_collection_for_select(Product.all, :id, :name), {}, class: "form-control" %>
    </div>
    <div class="field form-group">
      <%= f.label :selected_colors, "Color de producto", class: "form-label" %>
      <%= f.text_field :selected_colors, placeholder: "Color", class: "form-control" %>
    </div>
    <div class="field form-group">
      <%= f.hidden_field :campaign_id, value: @campaign.id, class: "form-control" %>
    </div>
    <div class="field form-group">
      <%= f.label :quantity, "Cantidad de item", class: "form-label" %>
      <%= f.number_field :quantity, value: @campaign.numberofitems, class: "form-control" %>
    </div>
    <div class="field form-group">
      <button class="btn btn-primary">
        <%= link_to_remove_association "Quitar item", f %>
      </button>
    </div>
  </div>
</div>

Params Whitelist

  def campaign_builder_params
    params.require(:campaign).permit(:campaign, :id, :status, :recipients_data, :selectedproducts => [], :selectedextras => [], :items_attributes => [:_destroy, :id, :product_id, :campaign_id, :price, :selected_colors, :selected_size, :quantity],
   end

  def items_params
    params.require(:item).permit(:status, :campaign, :purchasing_price, :price, :carrier, :delivery_date, :tracking_code, :is_design_made_by_client, :supplier, :selected_colors, :selected_size, :design_source_url)
  end

Upvotes: 0

Views: 63

Answers (1)

nathanvda
nathanvda

Reputation: 50057

I see a typo in your code, you write:

render partial: 'item_fields',  ff: ff

but in your partial you reference/use f.

So fix it as follows:

render partial: 'item_fields',  f: ff

Upvotes: 0

Related Questions