Rails Constant and Global Variables (Rails constants and Global Variables)

Source: Internet
Author: User
The following method defines constants and global variables in Rails. If you want to access any environment (development, testing, or product), it is defined in config/environment. rb.
For example:
BOOLEN_TRUE = 1
If you want your global variables to be determined by the Environment, configure
/Config/environments/development. rb
/Config/environments/test. rb
/Config/environments/production. rb

Note,
1> constants and global variables must all be in uppercase;
2> after global constants and variables are set, restart the server to load new settings.


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 wowould normally place something like this inApplication. cfm. In Rails I was uncertain how exactly to approach this. At first I thoughtApplication. rbFile 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 wocould be consistent regardless
Of environment (dev, test, and prod) I decided to place it in
Config/environment. rb. I placed this underRAILS_GEM_VERSIONConstant:

# 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.