2

I am using Rails 3 and have a need to run WEBrick with SSL support during development. To achieve this, I've followed this guide:

http://www.nearinfinity.com/blogs/chris_rohr/configuring_webrick_to_use_ssl.html

This works well, however, I want to ensure that these settings do no affect my rails application when run in production mode. We are currently using Apache/Passenger, and the project appears to still run fine. Is there a clean way, however, to make sure that this code isn't even executed? I'm thinking a possible answer could be an if/end block around the code, or perhaps a built-in rails facility that allows development-only code to be placed in a separate file or something similar.

YWCA Hello
  • 2,997
  • 4
  • 29
  • 40

1 Answers1

1

Looks like ENV['RAILS_ENV'] is your friend. The ENV hash shows you the Unix environment the app is being run under and Rails itself will look at RAILS_ENV to decide in which mode to run. You could do something like this:

if ENV['RAILS_ENV'].to_s == 'development' || ENV['RAILS_ENV'].to_s == ''
  # do your thing here
end

You can also make sure that you run webrick with that environment:

#> RAILS_ENV=development /path/to/webrick/script

Hope it helps.

aemadrid
  • 229
  • 1
  • 2
  • 7
  • I think that this answer is probably correct for older versions of rails, but not rails 3, as `RAILS_ENV` isn't _supposed_ to be used any longer (rails is started via `rails server [environment]` now). I've been using the following `if ARGV[1].nil?` which has been working so far, but I'd much prefer to be able to grab at a singular variable that is always set to the active environment. Perhaps that isn't available this early in the load process. – YWCA Hello Jan 12 '12 at 18:18
  • The code you are using there is using the ENV variable so it depends on that to work correctly. Honestly Webrick is almost never the solution though. – aemadrid Jan 19 '12 at 20:24