#55 Cleaning Up the View

This episode starts off with a big, messy template. Watch as this code shrinks and becomes more readable while the interface stays the same.
<!-- views/carts/show.rhtml -->
<% title "Shopping Cart" %>

<table id="line_items">
<tr>
<th>Product</th>
<th>Qty</th>
<th class="price">Unit Price</th>
<th class="price">Full Price</th>
</tr>
<%= render :partial => 'line_item', :collection => @cart.line_items %>
<tr>
<td class="total price" colspan="4">
Total: <%= number_to_currency @cart.total_price %>
</td>
</tr>
</table>

<!-- views/carts/_line_item.rhtml -->
<tr class="<%= cycle :odd, :even %>">
<td><%=h line_item.product.name %></td>
<td class="qty"><%= line_item.quantity %></td>
<td class="price"><%= free_when_zero(line_item.unit_price) %></td>
<td class="price"><%= free_when_zero(line_item.full_price) %></td>
</tr>
# models/line_item.rb
def full_price
unit_price*quantity
end

# models/cart.rb
def total_price
line_items.to_a.sum(&:full_price)
end

# helpers/carts_helper.rb
def free_when_zero(price)
price.zero? ? "FREE" : number_to_currency(price)
end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值