应用范围的全局变量 [英] application wide global variable
问题描述
在Rails中,我应该在哪里定义可以被每一层Rails堆栈识别的变量.
In Rails, where should I define the variable which can be recognized by every layer of Rails stacks.
例如,我想要一个 CUSTOMER_NAME='John'
变量,它可以在 helper、rake 任务、controller 和 model.我应该在 Rails 应用程序中的何处定义此变量?
For example, I would like to have a CUSTOMER_NAME='John'
variable which can be accessed in helper, rake task, controller and model. Where should I define this variable in Rails app?
我使用的是 Rails v2.3.2
推荐答案
在 /app/config/initializers
中的初始化程序中,这里的所有 .rb 文件都会被加载,我通常会创建一个名为首选项的文件.像这样的事情.
In an initializer in /app/config/initializers
all .rb files in here get loaded, I usually create one called preferences.rb for things like this.
参见:http://guides.rubyonrails.org/configuring.html#using-initializer-files
这篇关于应用范围的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!