depot购物车有些问题请教

hongxua 2008-07-23 01:02:31
本人现在正在学习RoR,在看Agile Web Development wiht Rails第2版,跟着做depot(进度第9章完成)。在做depot的购物车时有下面的问题:我添加一本书时是正常的,在添加统一本书时左边购物车的数目会增加1,而且也有动态效果。但是添加另外一本书时,购物车就成2行了,再添加一本书,就成3行了。重复添加,数目会增加1。
Your Pragmatic Cart
1× Progmatic Version Control $28.50
2× Test $30.90
1× Progmatic Version Control $28.50
2× Test $30.90
Total $59.40

上面是添加2种书时的情况,请教高手这是什么原因?
...全文
130 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
taito 2008-07-25
  • 打赏
  • 举报
回复
================
错了,这里
================
_cart_item.html.erb

<%for cart_item in @cart.items%> <== 去掉
<%if cart_item==@current_item%>
<tr id="current_item">
<%else%>
<tr>
<%end%>
<td> <%=cart_item.quantity%>× </td>
<td> <%=h(cart_item.title)%> </td>
<td class="item-price"> <%=number_to_currency(cart_item.price)%> </td>
</tr>
<%end%> <== 去掉

hongxua 2008-07-25
  • 打赏
  • 举报
回复
我有仔细看了一下书上的内容,发现在add_to_cart.html.erb中把循环逻辑抽取到局部模板中了,如下的代码:
<%=render(:partial=>"cart_item",:collection=>@cart.items)%>
所以在_cart_item.html.erb中就不需要再放“for...end”语句了,如果放了就是重复循环,所以才会出现上面的问题。
不知道这样理解是否正确。
taito 2008-07-25
  • 打赏
  • 举报
回复
这个是一个循环,会读出所有数据。在这里不需要。
hongxua 2008-07-25
  • 打赏
  • 举报
回复
谢谢taito,问题解决了。
另外,taito:能否解释一下为什么要去掉“for ...end”语句?谢谢。
hongxua 2008-07-24
  • 打赏
  • 举报
回复
添加同一本书时数量会+1,添加2本书时,就成4行了,添加3本书就是6行。内容会重复。代码如下:
Controller:
class StoreController < ApplicationController
def add_to_cart
begin
@product = Product.find(params[:id])
rescue ActiveRecord:: RecordNotFound
logger.error("Attempt to access invalid product #{params[:id]}")
#flash[:notice]="Invalid product"
redirect_to_index("Invalid product")
#redirect_to :action =>:index
else
@current_item=@cart.add_product(@product)
redirect_to_index unless request.xhr?
end
end
end

Model:
cart.rb
class Cart
attr_reader :items
def initialize
@items = []
end
def add_product(product)
current_item=@items.find{|item| item.product==product}
if current_item
current_item.increment_quantity
else
current_item=CartItem.new(product)
@items << current_item
end
current_item
end
def total_price
@items.sum{|item| item.price}
end
def total_items
@items.sum{|item| item.quantity}
end
end
cart_item.rb
class CartItem
attr_reader :product,:quantity
def initialize(product)
@product=product
@quantity=1
end
def increment_quantity
@quantity+=1
end
def title
@product.title
end
def price
@product.price * @quantity
end
end

View:
add_to_cart.rjs
page.select("div#notice").each {|div| div.hide}
page.replace_html("cart",:partial=>"cart",:object=>@cart)
page[:cart].visual_effect :blind_down if @cart.total_items == 1
page[:current_item].visual_effect :highlight,
:startcolor =>"#88ff88",
:endcolor=>"#114411"
_cart.html.erb
<div class="cart-title">Your Pragmatic Cart</div>
<table>
<%=render(:partial => "cart_item",:collection =>cart.items)%>
<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",:action=>:checkout%>
<%=button_to "Empty cart",:action=>:empty_cart%>
_cart_item.html.erb
<%for cart_item in @cart.items%>
<%if cart_item==@current_item%>
<tr id="current_item">
<%else%>
<tr>
<%end%>
<td><%=cart_item.quantity%>×</td>
<td><%=h(cart_item.title)%></td>
<td class="item-price"><%=number_to_currency(cart_item.price)%></td>
</tr>
<%end%>
xiaojing7 2008-07-23
  • 打赏
  • 举报
回复
意思是添加同一中书不会在数量上+1 而是加了一条数据》?
那就看你自己设计,可能你的思路有点问题!
taito 2008-07-23
  • 打赏
  • 举报
回复
代码贴出来先。view 和 controller

2,763

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ruby/Rails
社区管理员
  • Ruby/Rails社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧