Rails Constant and Global Variables(Rails的常量和全局变量)
2006-09-07 15:15
555 查看
在Rails下定义常量和全局变量的方法如下,如果你希望任何环境(开发,测试,产品)下都可以访问的话,则定义在config/environment.rb中
如:
BOOLEN_TRUE=1
如果你希望你的全局变量根据环境来决定,则根据需要在下面的环境中配置
/config/environments/development.rb
/config/environments/test.rb
/config/environments/production.rb
注意,
1> 常量和全局变量必须全部为大写;
2> 设置了全局常量和变量后需重新启动服务器以加载新的设置。
PS. english version
I was recently faced with a common problem that I wanted to share.
During development there are times you need to specifiy a global or
constant variable that will be shared throughout the entire
application. In ColdFusion you would normally place something like this in Application.cfm. In Rails I was uncertain how exactly to approach this. At first I thought an application.rb file made sense, but the more I thought about it I decided to take advantage of the config files.
Since my global variable was one that would be consistent regardless
of environment (dev, test, and prod) I decided to place it in
config/environment.rb. I placed this under the RAILS_GEM_VERSION constant:
# Specifies gem version of Rails to use when vendor/rails is not present
RAILS_GEM_VERSION = ‘1.1.2′
BASE_URL = ‘http://mydomain.com/’
Now referencing this in any view can be done similar to any other variable:
<%= BASE_URL %>
If you need to specify globals that are specific to environment then I recommend placing them in their appropriate config file:
/config/environments/development.rb
/config/environments/test.rb
/config/environments/production.rb
From: http://www.db75.com/new_blog/?p=262
如:
BOOLEN_TRUE=1
如果你希望你的全局变量根据环境来决定,则根据需要在下面的环境中配置
/config/environments/development.rb
/config/environments/test.rb
/config/environments/production.rb
注意,
1> 常量和全局变量必须全部为大写;
2> 设置了全局常量和变量后需重新启动服务器以加载新的设置。
PS. english version
I was recently faced with a common problem that I wanted to share.
During development there are times you need to specifiy a global or
constant variable that will be shared throughout the entire
application. In ColdFusion you would normally place something like this in Application.cfm. In Rails I was uncertain how exactly to approach this. At first I thought an application.rb file made sense, but the more I thought about it I decided to take advantage of the config files.
Since my global variable was one that would be consistent regardless
of environment (dev, test, and prod) I decided to place it in
config/environment.rb. I placed this under the RAILS_GEM_VERSION constant:
# Specifies gem version of Rails to use when vendor/rails is not present
RAILS_GEM_VERSION = ‘1.1.2′
BASE_URL = ‘http://mydomain.com/’
Now referencing this in any view can be done similar to any other variable:
<%= BASE_URL %>
If you need to specify globals that are specific to environment then I recommend placing them in their appropriate config file:
/config/environments/development.rb
/config/environments/test.rb
/config/environments/production.rb
From: http://www.db75.com/new_blog/?p=262
相关文章推荐
- Python全局变量的隐藏“窍门”(CONSTANT,global)
- 关于namespace或global作用域内变量和常量以及static修饰符
- PHP 全局变量 局部变量 静态变量 常量
- dx编译shader报错:global variables are implicitly constant, enable compatibility mode to...
- PHP Global定义全局变量使用说明
- error X3025: global variables are implicitly constant, enable compatibility mode to allow modification
- C/C++定义全局变量/常量几种方法的区别
- C/C++定义全局变量/常量几种方法的区别
- iOS定义静态变量、静态常量、全局变量
- 定义C/C++全局变量/常量几种方法的区别
- Python 专题六 局部变量、全局变量global、导入模块变量
- 数据内存存储(常量,局部变量,全局变量,程序代码)
- 飘逸的python - 为什么修改全局的dict变量不用global关键字
- php中的变量,常量,全局变量,超全局变量
- Python学习笔记(7):global关键字及全局变量的用法
- [置顶] 跟我学AngularJS:全局变量设置之value vs constant vs rootscope vs 服务
- JS避免太多全局常量变量(泄露)和善于使用数组容器
- AngularJS 定义全局变量方法module.value()和module.constant()
- PHP中全局变量global和$GLOBALS[]的区别分析
- 程序代码,常量,局部变量,全局变量在内存中的保存位置