Global Constants
It happens occasionally–you have a small piece of data you need to share across every page in your site. Or some configuration value that you want to set in one place.
So how do you do it?
The official Rails wiki cites four different ways to add configuration info into your application. One way is your own config file, which may prove useful if you have several things to declare.
But what if you just want something simple–maybe the name of your site, which you haven’t yet finalized?
Simply open up your config/environment.rb file, and add a constant, like so:
SITE_NAME = 'RailsRocket'
And voila, you’re set! To use the constant, simply output SITE_NAME anywhere in your application, such as between the title tags in your site-wide layout file.
For more information, check out the How to Add Your Own Config Info page on the Rails wiki.
Tags: global variables, introductory Posted in


Leave a Reply