Ruby on rails开发从头来(windows)(十)-清空购物车和格式化金额
2009-08-06 19:23
621 查看
在上次的内容里,我们给购物车添加了错误处理,这次来实现清空购物车和金额的格式化处理。
到现在我们还没有给显示购物信息列表页面的“empty cart”链接添加任何处理。我们首先来实现这个功能:
1. 在Store_Control.rb文件中添加empty_cart方法:
2. 接下来是cart.rb文件,添加下面的代码:
好了,就是这么简单,现在点击empty cart链接,会重新定位到index页面,并且显示一个消息,如图:
嗯,到这里你肯定看到store_controller中关于显示异常信息的flash的操作部分有重复代码,这是代码的坏味道,现在我们使用Extract Method,将这些代码提取到一个方法中,下面是修改后的代码,我们修改了add_to_cart,display_cart,empty_cart三个方法,并且添加了一个redirect_to_index方法:
另外,cart.rb里也有重复的代码,我们重构一下:
整理完了代码,再来看看另一个小功能,格式化金额:
1. 在rails_appsdepotapphelpers目录下的application_helper.rb中添加代码:
2. 修改rails_appsdepotappviewsstore目录下的display_cart.rhtml文件中的两行:
和
变为
和
现在可以看看最后的结果了,如图:
到现在我们还没有给显示购物信息列表页面的“empty cart”链接添加任何处理。我们首先来实现这个功能:
1. 在Store_Control.rb文件中添加empty_cart方法:
def empty_cart find_cart.empty! flash[:notice] = 'Your cart is now empty' redirect_to(:action => 'index') end
2. 接下来是cart.rb文件,添加下面的代码:
def empty! @items = [] @total_price = 0.0 end
好了,就是这么简单,现在点击empty cart链接,会重新定位到index页面,并且显示一个消息,如图:
嗯,到这里你肯定看到store_controller中关于显示异常信息的flash的操作部分有重复代码,这是代码的坏味道,现在我们使用Extract Method,将这些代码提取到一个方法中,下面是修改后的代码,我们修改了add_to_cart,display_cart,empty_cart三个方法,并且添加了一个redirect_to_index方法:
def add_to_cart product = Product.find(params[:id]) @cart = find_cart @cart.add_product(product) redirect_to(:action => 'display_cart') rescue logger.error("Attempt to access invalid product #{params[:id]}") flash[:notice] = 'Invalid product' redirect_to_index('Invalid product') end def display_cart @cart = find_cart @items = @cart.items if @items.empty? redirect_to_index("Your cart is currently empty") end end def empty_cart find_cart.empty! redirect_to_index('Your cart is now empty') end def redirect_to_index(msg = null) flash[:notice] = msg redirect_to(:action => 'index') end
另外,cart.rb里也有重复的代码,我们重构一下:
def empty! @items = [] @total_price = 0.0 end def initialize empty! end
整理完了代码,再来看看另一个小功能,格式化金额:
1. 在rails_appsdepotapphelpers目录下的application_helper.rb中添加代码:
def fmt_dollars(amt) sprintf("$%0.2f", amt) end
2. 修改rails_appsdepotappviewsstore目录下的display_cart.rhtml文件中的两行:
<td align="right"><%= item.unit_price %></td> <td align="right"><%= item.unit_price * item.quantity %></td>
和
<td id="totalcell"><%= (@cart.total_price)%></td>
变为
<td align="right"><%= fmt_dollars(item.unit_price) %></td> <td align="right"><%= fmt_dollars(item.unit_price * item.quantity) %></td>
和
<td id="totalcell"><%= fmt_dollars(@cart.total_price)%></td>
现在可以看看最后的结果了,如图:
相关文章推荐
- Ruby on rails开发从头来(windows)(十)-清空购物车和格式化金额
- Ruby on rails开发从头来(windows)(十)-清空购物车和格式化金额
- Ruby on rails开发从头来(windows)(八)-使用Session创建购物车
- Ruby on rails开发从头来(windows)(八)-使用Session创建购物车
- Ruby on rails开发从头来(windows)(九)-给购物车添加错误处理
- Ruby on rails开发从头来(windows)(八)-使用Session创建购物车(修正原版错误)
- Ruby on rails开发从头来(windows)(九)-给购物车添加错误处理
- Ruby on rails开发从头来(windows)(八)-使用Session创建购物车
- Ruby on rails开发从头来(windows)(八)-使用Session创建购物车
- Ruby on rails开发从头来(windows)(九)-给购物车添加错误处理 (修正原版错误)
- Ruby on rails开发从头来(windows)(九)-给购物车添加错误处理
- Ruby on rails开发从头来(windows)(十八)-自动生成文档
- Ruby on rails开发从头来(windows)(一)-使用InstantRails快速搭建Ruby On Rails开发环境
- Ruby on rails开发从头来(windows)(四)-第一个添删查改例子
- Ruby on rails开发从头来(windows)(十三)-订单(Order)
- Ruby on rails开发从头来(windows)(十三)-订单(Order)
- Ruby on rails开发从头来(windows)(三十一)- Rails的目录结构
- Ruby on rails开发从头来(windows)(二十六)- 使用Mock对象
- Ruby on rails开发从头来(windows)(十七)-控制访问权限
- Ruby on rails开发从头来(windows)(二十八)- 使用Rake运行测试