打印

有关You have a nil object when you didn't expect it!请教

有关You have a nil object when you didn't expect it!请教

学AWDWR第二版第九章Ajzx时,想迁移购物车,但出现以下问题,google了好久都没找到答案,望指教。

NoMethodError in Store#index
Showing app/views/store/_cart_item.rhtml where line #2 raised:

You have a nil object when you didn't expect it!
The error occurred while evaluating nil.quantity

Extracted source (around line #2):

1: <tr>
2:   <td><%= cart_item.quantity  %> & times; </td>
3:   <td><%= h(cart_item.title) %></td>
4:   <td class="item-price" ><%= number_to_currency (cart_item.price) %></td>
5: </tr>

Trace of template inclusion: /app/views/store/_cart.rhtml, /app/views/layouts/store.rhtml

RAILS_ROOT: ./script/../config/..

Application Trace | Framework Trace | Full Trace
#{RAILS_ROOT}/app/views/store/_cart_item.rhtml:2:in `_run_rhtml_47app47views47store47_cart_item46rhtml'
#{RAILS_ROOT}/app/views/store/_cart.rhtml:3:in `_run_rhtml_47app47views47store47_cart46rhtml'
#{RAILS_ROOT}/app/views/layouts/store.rhtml:15:in `_run_rhtml_47app47views47layouts47store46rhtml'

ps:用 rake db:sessions:clear  清理session也不行。

TOP

cart_item对象为nil

请检查下

然后就是
_cart_item.rhtml

应该是你render进来的吧 检查传入参数是否正确
复制内容到剪贴板
代码:
:collection =>? 或者 :object =>?
PS:我没有看你那个书 只是看着你上面的错误提示猜的

TOP

谢谢了  弄好了

TOP