[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