书中有很多关于这个话题的问题(第11章),但似乎没有一个问题和我所经历的一样。我遇到的问题是,单击buy按钮没有更新侧边栏中的ajax购物车,点击刷新会使其更新。看一下服务器日志,我可以看到一切似乎都是正确的-
Started POST "/line_items?product_id=1" for 127.0.0.1 at 2014-07-08 17:47:36 +0100
Processing by LineItemsController#create as JS
Parameters: {"authenticity_token"=>"O+QFilrdqd/AsAoJGHzayWphHT8PDBEBqnjFIAXqQ7Y=", "product_id"=>"1"}
Cart Load (0.1ms) SELECT "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT 1 [["id", 6]]
Product Load (0.1ms) SELECT "products".* FROM "products" WHERE "products"."id" = ? LIMIT 1 [["id", 1]]
LineItem Load (0.1ms) SELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = ? AND "line_items"."product_id" = 1 LIMIT 1 [["cart_id", 6]]
(0.1ms) begin transaction
SQL (0.3ms) UPDATE "line_items" SET "quantity" = ?, "updated_at" = ? WHERE "line_items"."id" = 10 [["quantity", 7], ["updated_at", "2014-07-08 16:47:36.666376"]]
(1.5ms) commit transaction
LineItem Load (0.1ms) SELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = ? [["cart_id", 6]]
Product Load (0.1ms) SELECT "products".* FROM "products" WHERE "products"."id" = ? LIMIT 1 [["id", 1]]
Product Load (0.1ms) SELECT "products".* FROM "products" WHERE "products"."id" = ? LIMIT 1 [["id", 2]]
Rendered line_items/_line_item.html.erb (1.9ms)
Rendered carts/_cart.html.erb (4.3ms)
Rendered line_items/create.js.erb (5.4ms)
Completed 200 OK in 14ms (Views: 7.3ms | ActiveRecord: 2.4ms)app/views/carts/_cart.html.erb
<h2>Your cart<h2>
<table>
<%= render(cart.line_items) %>
<tr class="total_line">
<td colspan="2">Total</td>
<td class="total_cell"><%= quid(cart.total_price) %></td>
</tr>
</table>
<%= button_to 'Empty cart', cart, method: :delete, data: { confirm: 'Are you sure?' }%>app/views/line_items/_line_item.html.erb
<tr>
<td><%= line_item.quantity %>×</td>
<td><%= line_item.product.title %></td>
<td class="item_price"><%= quid(line_item.total_price) %></td>
</tr>app/view/line/create.js.erb
$('#cart').html('<%= escape_javascript render(@cart) %>');
$('#cart').html(alert("alert"));购物车不会更新,但是警报会触发。
发布于 2014-07-09 12:59:59
因此,在遵循@deep的其他优秀建议尝试诊断问题之后,我遵循了他的最后一条建议,逐行查看了每一个相关的文件,在app/views/layouts/application.html.erb中,这是一个缺失的<div id="cart">,我在编辑该文件时一定已经删除了它。吸取的教训-谢谢你@深。
https://stackoverflow.com/questions/24637594
复制相似问题