jeudi 9 mars 2017

Multiple form in rails

Here I am trying to create a single form through which I want to save multiple data by one key press. For this I have created form in tabular format.

enter image description here

When i try to add date it is supplied inform of array to controller as below:-

enter image description here

But it gets rolled back if i try to save it. Here i am using nested form so that i can save multiple data to second form.

def new
    @purchase = Purchase.new
    @purchase_item = @purchase.purchase_items.build
    @item = Item.order(:name)
  end

  def create
    @purchase = Purchase.new(purchase_params)
    if @purchase.save
      flash[:success] = "Items added."
    else
      flash[:error] = "Items not added."
    end
    redirect_to :new_purchase
  end
 def purchase_params
    params.require(:purchase).permit(:vendor_id, purchase_items_attributes: [ :purchase_id , :item_id , :quantity, :unit_price ])
  end

So, Can anyone help me to solve this problem.

Aucun commentaire:

Enregistrer un commentaire