我正在使用Rails进行敏捷Web开发的章节练习。这是一个商店,产品可以添加到购物车。购物车内的产品称为LineItem。您可以删除购物车内的各个订单项。删除最后一个订单项时,我希望销毁整个购物车。我的代码不起作用:
CartsController
def destroy
@cart = current_cart
@cart.destroy
session[:cart_id] = nil
respond_to do |format|
format.html { redirect_to store_path }
format.json { head :no_content }
end
end
LineItemsController
def destroy
@cart = current_cart
@line_item = LineItem.find(params[:id])
quantity = @line_item.quantity
if quantity > 1
quantity -= 1
@line_item.update_attribute(:quantity, quantity)
else
@line_item.destroy
end
respond_to do |format|
format.html {
if @cart.line_items.empty?
@cart.destroy
else
redirect_to @cart
end
}
format.json { head :no_content }
end
end
这会产生以下错误:
Template is missing
Missing template line_items/destroy, application/destroy with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "/home/mike/Projects/depot/app/views"
当购物车被销毁时,我想最终到达store_path。
感谢, 麦克
答案 0 :(得分:0)
请参阅添加(作为评论):
if @cart.line_items.empty?
@cart.destroy
# redirect_to store_path
else