i am creating a simple shopping cart in rails, when I add the product to cart i get this error : You have a nil object when you didn't expect it!
the add to cart method is :
def add_to_cart
begin
product = Product.find(params[:id])
rescue ActiveRecord::RecordNotFound
logger.error("Attemp to access invalid product #{params[:id]}")
flash[:notice] = "Invalid Product !"
redirect_to :action => :index
else
@cart = find_cart
@cart.add_product(product)
end
end
and the add_product in cart :
def add_product(product)
current_item = @items.find {|item| item.product == product}
if current_item
current_item.increment_quantity
else
@items << CartItem.new(product)
end
end
the cart was working properly, when I add the rescue method to add_to_cart this happened...