应用范围的全局变量 [英] application wide global variable

查看:66
本文介绍了应用范围的全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rails中,我应该在哪里定义可以被每一层Rails堆栈识别的变量.

In Rails, where should I define the variable which can be recognized by every layer of Rails stacks.

例如,我想要一个 CUSTOMER_NAME='John' 变量,它可以在 helperrake 任务、controllermodel.我应该在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆