I have a list of products, each product has a button to add to the cart, which redirects to the path of the line_items controller.

Action code from line_items_controller.rb

def create item = Item.find(params[:item_id]) @line_item = @cart.line_items.build(item: item) respond_to do |format| if @line_item.save format.html { redirect_to @line_item.cart, notice: 'Line item was successfully added.' } format.json { render :show, status: :created, location: @line_item } else format.html { render :new } format.json { render json: @line_item.errors, status: :unprocessable_entity } end end end 

What hash should be transferred to .build () to add the item to the cart?

In theory, it is necessary to transfer the hash of the parameters of the goods, but no matter how I tried to transfer it, it does not work. Now gives the error can't write unknown attribute item_id

    1 answer 1

    Problem solved. The bottom line was the incorrect line_items migration file , because specified invalid name.

    Instead of t.references :item was written t.references :product

    So I did rake db:rollback and fixed the migration file.