I am trying to create a basic setup in my cart controller that will allow me to incrementally increase the quantity value of products added to the cart if an Item record already exists.
I currently have:
class ItemsController < ApplicationController
def create
@product = Product.find(params[:product_id])
if @item.new_record?
@item = Item.create!(:cart => current_cart, :product => @product, :quantity => 1, :unit_price => @product.price)
else
@item.increment! :quantity
end
redirect_to cart_path(current_cart.id)
end
end
However I keep getting the error undefined methodnew_record?’ for nil:NilClass`
Any help people can offer to solve this really would be much appreciated!
you haven’t anywhere declared @item. before this thats why the error is coming
try this code
This should solve your problem