Rails Environments Gotcha
I’ve been working on an application that sends emails. (You’ll hear more about the application very soon) The emails often contains links to the application. In development and test I want these links to point to localhost:3000, and in production I want them to point to mydomainname.com. Therefore, the ActionMailer class needs to know the root of the application.
So, in environment.rb I added the following line:
APPLICATION_ROOT = "http://localhost:3000"
And in production.rb I added the following line:
APPLICATION_ROOT = "http://mydomainname.com"
Unfortunately, when I deployed to production the emails were being sent with http://localhost:3000 as the base for links.
I checked my mongrel config and everything seemed to be set up right for production. I checked the logs and saw that the application was running as production.
Then I ran
script/console production, saw the following error message, and immediately got an idea what I did wrong.
production.rb:19: warning: already initialized constant APPLICATION_ROOT
APPLICATION_ROOT was still returning ‘http://localhost:3000’
The problem was I was counting on settings in production.rb to override those in environment.rb. And they do except I defined my setting as a CONSTANT.
I moved the constant declaration out of the environment.rb and into both development.rb and test.rb and everything now works fine.
The thing I still don’t understand is that according to page 330 of the PickAxe Ruby is supposed to allow you to alter the value of a constant.
Anyone know why the constant’s value didn’t change in my config files?
More Articles on Software & Product Development
- Agile With a Lowercase “a”
- ”Agile“ is an adjective. It is not a noun. It isn’t something you do, it is something you are.
- How Do You End Up With A Great Product A Year From Now?
- Nail the next two weeks. 26 times in a row.
- Build it Twice
- Resist the urge to abstract until you've learned what is general to a class of problems and what is specific to each problem.