2013-12-18 48 views
0

我正在尝试创建一个会话。我的动作看起来是这样的:ProductCategoriesController中的NoMethodError#index

class ApplicationController < ActionController::Base 
    # Prevent CSRF attacks by raising an exception. 
    # For APIs, you may want to use :null_session instead. 
    protect_from_forgery with: :exception 

    def prepare_cart 
    session[:cart] = {} if session[:cart].empty? 
    end 
end 

为了这个,我正在错误:

NoMethodError in ProductCategoriesController#index 
undefined method `empty?' for nil:NilClass 

不能找到了什么的probem。它第一次运作良好。当我重新浏览器时,这个错误开始出现。请帮忙!

回答

2

你的环节没有:购物车钥匙首先检查空虚。

试试这个:

session[:cart] ||= {} 
+0

感谢男人..工作就像一个宝石.. –

0

会话[:车]是零,这样你得到零错误,请检查零状态。

相关问题