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.

About Ashiq Alibhai

Ashiq Alibhai, PMP, has been a Rails aficionado since 2007, and developed web applications since early 2003, where he learned PHP in one summer. As the driving-force behind RailsRocket and the Launchpad project, he seeks to share the ease of development with Rails far and wide.
This entry was posted in Development and tagged , . Bookmark the permalink.

Comments are closed.