运行 Rails 3.1 Ruby 1.92
我正在尝试使用 AJAX 更新我的购物车。我的店面有以下按钮来创建订单项
<%= button_to " Buy ", line_items_path(product_id: product), remote: true, :class =>"blue-btn" %>
以及我的 line_items 控制器中的以下创建操作
def create
@cart = current_cart
product = Product.find(params[:product_id])
@line_item = @cart.add_product(product.id)
respond_to do |format|
if @line_item.save
format.html { redirect_to store_url,notice: 'your item was added.' }
format.js { @current_item = @line_item }
format.json { render json: @line_item,
status: :created, location: @line_item }
最后,我在 app/views/line_items 目录中的 create.js.erb 模板中有以下 RJS 代码:
$('#cart').html("<%=j render @cart %>");
但是,当我将商品添加到购物车时,我必须刷新页面才能查看更新的数量和总数。每次尝试将商品添加到购物车后,我都会检查服务器日志,并显示它呈现 line_items/create.js.erb 我已在下方包含完整日志,任何帮助将不胜感激
在 2013-08-04 14:01:26 -0400 开始 POST "/line_items?product_id=1" for 127.0.0.1 由 LineItemsController#create as JS 参数处理:{"authenticity_token"=>"2TxUMUloihqhzGHlZrC9YKNPGNhfwZ070lEW64uL0SY=", "product_id" =>"1"} ←[1m←[35mCart Load (1.0ms)←[0m SELECT "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT 1 [["id", 64]] ←[1m←[36mProduct Load (0.0ms)←[0m ←[1mSELECT "products".* FROM "products" WHERE "products"."id" = ? LIMIT 1←[0m [["id" , "1"]] ←[1m←[35mLineItem Load (0.0ms)←[0m SELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = 64 AND “line_ items”.“product_id”