专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Ruby/Rails

一个有关问题

发布时间:2011-06-29 18:40:37 文章来源:www.iduyao.cn 采编人员:星星草
一个问题
敏捷开发书中代码是:
<div class="cart-title">Your Cart</div>
<table>
<% for cart_item in @cart.items %>
<tr>
<td><%= cart_item.quantity %>&times;</td>
<td><%= h(cart_item.title) %></td>
<td class="item-price"><%= number_to_currency(cart_item.price) %></td>
</tr>
<% end %>
<tr class="total-line">
<td colspan="2">Total</td>
<td class="total-cell"><%= number_to_currency(@cart.total_price) %></td>
</tr>
</table>
<%= button_to "Empty cart" , :action => :empty_cart %>

但是我用这个不行。用下面这个才行。
-------------------我的代码
<div class="cart-title">Your Cart</div>
<table>
  <% for item in @cart.items %>
<tr>
  <td><%= item.quantity %>&times;</td>
  <td><%= h item.title %></td>
  <td class="item-price"><%= number_to_currency(item.price) %></td>
</tr>
<% end %>
<tr class="total-line">
  <td colspan="2">Total :</td>
  <td class="total-cell"><%= number_to_currency(@cart.total_price) %></td>
</tr>
</table>
<%= button_to "Empty cart" , :action => :empty_cart %> 

也就是<% for cart_item in @cart.items %> 跟 <% for item in @cart.items %>;
<td><%= cart_item.quantity %>&times;</td>
<td><%= h(cart_item.title) %></td>
<td class="item-price"><%= number_to_currency(cart_item.price) %></td> 跟
<td><%= item.quantity %>&times;</td>
  <td><%= h item.title %></td>
  <td class="item-price"><%= number_to_currency(item.price) %></td>
</tr>的区别,但是为什么@cart.total_price都需要一样呢,如果单单是@total_price就不行了。

------解决方案--------------------
你这个代码好复杂,看不见你的问题。
你检查一下app/models下的文件,看里面有没有cart_item或者item的类定义文件。
total_price是cart类的变量,而不是全局变量,所以要用cart.total_price。
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: