【发布时间】:2012-11-19 21:16:39
【问题描述】:
我为我的应用程序购物车创建了一个部分 (views/cart/_cart.html.erb)。我只在我的 views/layouts/application.html.erb 文件中呈现它。但是当我运行它时,除了 views/cart/_cart.index.erb 之外的每个页面上都会出现 NoMethodError,我怀疑这是因为它与正在渲染的部分共享相同的控制器。
我一直在到处寻找答案,并相信这与我在渲染部分时传递变量的方式有关,如果有人可以看看,我将不胜感激?
所有代码都可以在这里找到:https://github.com/rossmc/topsnowboards
我的部分代码 (views/cart/_cart.html.erb) 开始:
<% @cart = cart%>
<h1>Your Cart</h1>
<% if @cart.empty? %>
<p>There is nothing in your shopping Cart</p>
<% end %>
<% total = 0 %>
<!-- More code displaying the rest of the cart omitted for brevity
部分在 views/layouts/application.html.erb 中呈现:
<%= render :partial => "/cart/cart", :locals => { :cart => @cart }
错误消息当我运行 Rails 时,它向我抛出:
NoMethodError in Site#home
Showing Server Side/PROJ-Server Side/app/views/cart/_cart.html.erb where line #5 raised:
undefined method `empty?' for nil:NilClass
Extracted source (around line #5):
2:
3: <h1>Your Cart</h1>
4:
5: <% if @cart.empty? %>
6: <p>There is nothing in your shopping Cart</p>
7: <% end %>
8:
【问题讨论】:
标签: ruby-on-rails model-view-controller local-variables renderpartial partials