ruby-on-railsruby-on-rails-5nested-formscocoon-gem

Nested form with cocoon rails duplicates child items when editing


The nested form works great when creating new records. However when I edit the record, it creates duplicate child items even if I do not mean to add new items.

How do I prevent it from creating new child items when updating the Sales Order?

class SalesOrderItem < ApplicationRecord
  belongs_to :item
  belongs_to :sales_order

class SalesOrder < ApplicationRecord
  has_many :sales_order_items, dependent: :destroy, inverse_of: :sales_order
  accepts_nested_attributes_for :sales_order_items


Sales Order Controller (parent)

  def new
    @sales_order = SalesOrder.new
    @items = Item.all
    @customers = Customer.all
    @sales_order.sales_order_items.build
  end

  def edit
  end

  def update
    respond_to do |format|
      if @sales_order.update(sales_order_params)
        format.html { redirect_to @sales_order, notice: 'Sales order was successfully updated.' }
        format.json { render :show, status: :ok, location: @sales_order }
      else
        format.html { render :edit }
        format.json { render json: @sales_order.errors, status: :unprocessable_entity }
      end
    end
  end

-----------------------------------------------
def sales_order_params
      params.require(:sales_order).permit(:customer_id,
                                          :date,
                                          :user_id,
                                          :remarks,
                                          :vat_ex,
                                          :sales_order_items_attributes => [:item_id, :price, :quantity]
                                          )
 end

Solution

  • To let rails know you're editing an existing sales_order_item you need to provide its id in the request, otherwise it'll think you mean to create a new one.

    So you need to allow the id in the subparameters: :sales_order_items_attributes => [:id, ...]

    And make sure you are submitting it in the form that triggers this action.