[b]局部模板_cart_item.rhtml[/b]
[b]控制器中修改成[/b]:
[b]模型cart.rb对应内容修改成[/b]:
[b]视图下建立decrease_cart_item.rjs文件,内容[/b]:
这样做的效果是:当前行的采购数量减少时,当前行也会用高亮渐变效果显示。如果是该行的数量被减少到0,那么该行就会被删除,而不再能对它使用渐变效果。
<% 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_remote "减少",
:url => {:action => "decrease_cart_item",:id => cart_item.product} %>
</td>
</tr>
[b]控制器中修改成[/b]:
def decrease_cart_item
product = Product.find(params[:id])
@cart = session[:cart]
@current_item = @cart.descrease_quantity(product)
redirect_to_index unless request.xhr?
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) #从数组中删除这个对象
current_item = nil #因为该对象已经从数组中删除了,后面程序要进行判断是否存在,所以要把它的值赋值为nil
end
current_item
end
[b]视图下建立decrease_cart_item.rjs文件,内容[/b]:
page[:cart].replace_html :partial => 'cart', :object => @cart
if @current_item
page[:current_item].visual_effect :highlight,:startcolor => "#88ff88",:endcolor => "#114411"
end
这样做的效果是:当前行的采购数量减少时,当前行也会用高亮渐变效果显示。如果是该行的数量被减少到0,那么该行就会被删除,而不再能对它使用渐变效果。