在拥有较长的 PHP 背景之后,我对 Ruby 和 Rails 还是很陌生。我遇到了一个奇怪的错误,这让我很困惑。
我有一个像这样设置的类变量:
class Article < ActiveRecord::Base
@@customTags = []
def self.addCustomGA(slot, name, value, scope=3)
h = {:slot => slot, :name => name, :value => value, :scope => scope}
@@customTags.push h
end
def self.customTagOutput
tags = ""
@@customTags.each do |ct|
tags << "_gaq.push(['_setCustomVar', "+ct[:slot].to_s+", '"+ct[:name].to_s+"', '"+ct[:value].to_s+"', "+ct[:scope].to_s+"]);\n"
end
tags
end
end
我在我的控制器中运行一个小测试,调用:
Article.addCustomGA(1, 'categories', @article.category_list);
然后在我的 HAML 视图中,我正在输出如下内容:
#{Article.customTagOutput}
这在第一次运行时效果很好。但是当我刷新页面时,它会不断向数组中添加一个条目 - 所以@@customTags
数组似乎在我的会话中持续存在!如果我几分钟不刷新,它就会自行重置。
我的第一个想法是发生了某种缓存的事情,但谷歌并没有为我提供任何东西。