我一直在关注“使用Rails进行敏捷Web开发”一书中的指南。第11章“添加一个Ajax短跑”。 我不知道为什么当我点击添加到购物车3次时,左侧边栏上显示3个购物车,如果我点击4次,则显示4个购物车......
我不知道这个错误在哪里。
application.html.erb
<% if @cart %>
<%= hidden_div_if(@cart.line_items.empty?, :id => 'cart') do %>
<%= render @cart %>
<% end %>
<% end %>
存储/索引
<%= button_to 'Add to cart', line_items_path(:product_id => product),
:remote => true %>
_line_item.html.erb
<div class="cart_title">Your Cart</div>
<table>
<% for item in @cart.line_items %>
<% if line_item == @current_item %>
<tr id="current_item">
<% else %>
<tr>
<% end %>
<tr>
<td><%= item.quantity %>×</td>
<td><%= item.product.title %></td>
<td class="item_price"><%= number_to_currency(item.total_price) %></td>
</tr>
<% end %>
<tr class="total_line">
<td colspan="2">Total</td>
<td class="total_cell"><%= number_to_currency(@cart.total_price) %></td>
</tr>
</table>
<%= button_to "Checkout", new_order_path, :method => :get %>
<%= button_to 'Empy cart', @cart, :method => :delete,
:confirm => 'Are you sure?' %>
_cart.html.erb
<%= render(cart.line_items) %>
/ -------- ----------边栏 /
您的购物车
1×Book1 $ 10.00
1×Book2 $ 10.00 总计$ 20.00
您的购物车
1×Book1 $ 10.00
1×Book2 $ 10.00 总计$ 20.00
您的购物车
1×Book1 $ 10.00
1×Book2 $ 10.00 总计$ 20.00
主
订单
产品
用户