[英]I can't figure out my rails line_items controller create action error?
尝试将产品添加到购物车时出现此错误:
ActiveRecord::RecordNotFound in LineItemsController#create
Couldn't find Cart with 'id'=2
所以create动作有问题,但是我不确定它是什么... Create动作:(在我的实际代码中@product和数量之间的差距不存在,无法正确设置其格式。)
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)
结束
来自应用程序控制器的current_cart方法:
def current_cart
session[:cart_id] ||= Cart.create!.id
@current_cart ||= Cart.find(session[:cart_id])
end
谢谢您的帮助。
您的代码在ApplicationController#current_cart方法中失败。 会话中是否已有购物车ID? 如果是这样,代码将尝试查找具有该ID的购物车记录,并且如果该记录已被删除,则失败,并显示上述消息。
因此,首先要弄清楚会话中的内容。 其次,找出如何在不提供应用程序过时或无效数据的情况下创建新购物车。
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.