我有一个帮助方法来获取我的应用程序控制器中的当前购物车:
class ApplicationController < ActionController::Base
protect_from_forgery
helper :all # include all helpers, all the time
private
def current_cart
if session[:cart_id]
@current_cart ||= Cart.find(session[:cart_id])
session[:cart_id] = nil if @current_cart.purchased_at
end
if session[:cart_id].nil?
@current_cart = Cart.create!
session[:cart_id] = @current_cart.id
end
@current_cart
end
end
Run Code Online (Sandbox Code Playgroud)
我可以从大多数视图中调用该方法,但我想在views/layout/application.html.erb文件中使用它,如下所示:
<div id="cart_menu">
<ul>
<li>
<%= image_tag("cart.jpg")%>
</li>
<li>
<%= link_to "#{current_cart.number_of_items}", current_cart_url %>
</li>
<li>
<a href="/checkout/">Checkout</a>
</li>
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)
但是当我尝试的时候,我得到了一个
undefined local variable or method `current_cart' for #<#<Class:0x2d2d834>:0x2d2b908>
Run Code Online (Sandbox Code Playgroud)
错误..
任何想法为什么?