Reputation: 155
I am getting this error when trying to add a product to a cart:
ActiveRecord::RecordNotFound in LineItemsController#create
Couldn't find Cart with 'id'=2
So something is wrong with the create action, but I'm not sure what it is... Create action:(the gap between @product and quantity isn't there in my actual code, can't get it formatted right.)
def create
@product = Product.find(params[:product_id])
@line_item = LineItem.create!(:cart => current_cart, :product => @product,
:quantity=> 1, :unit_price => @product.price)
flash[:notice] = "Added #{@product.name} to cart."
redirect_to cart_url(current_cart)
end
current_cart method from application controller:
def current_cart
session[:cart_id] ||= Cart.create!.id
@current_cart ||= Cart.find(session[:cart_id])
end
Thanks for the help.
Upvotes: 0
Views: 55
Reputation: 8212
Your code is failing in the ApplicationController#current_cart method. Do you already have a cart ID in the session? If so, the code will try to find a Cart record with that ID and if it's been deleted, it'll fail with the above message.
So, firstly, figure out what you have in your session. Secondly, figure out how to create a new cart without supplying your application stale or invalid data.
Hope this helps.
Upvotes: 1