[b]局部模板_cart_item.rhtml[/b]
[b]控制器中加入[/b]:
[b]模型cart.rb中加入[/b]:
<% if cart_item == @current_item %>
<tr id="current_item" class="cart-item">
<% else %>
<tr class="cart-item">
<% end %>
<td width="5%"><%= cart_item.quantity %>*</td>
<td width="70%"><%= h cart_item.title %></td>
<td width="12%">¥<%= cart_item.price%></td>
<td width="13%">
<%=link_to '减少',:action=>:decrease_cart_item,:id=>cart_item.product %></td>
</tr> [b]控制器中加入[/b]:
def decrease_cart_item
product = Product.find(params[:id])
@cart = session[:cart]
@cart.descrease_quantity(product)
redirect_to_index
end[b]模型cart.rb中加入[/b]:
def descrease_quantity(product)
current_item = @items.find {|item| item.product == product}
if current_item.quantity>1
current_item.descrease_quantity
else
@items.delete(current_item) #从数组中删除这个对象
end
end
本文介绍了一种在购物车中实现商品数量减少功能的方法。通过在控制器中定义 decrease_cart_item 方法并结合模型 cart.rb 中的 decrease_quantity 方法,可以有效地减少指定商品的数量。此功能涉及对商品对象的操作和页面显示的更新。
144

被折叠的 条评论
为什么被折叠?



