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.

2
  • The Rails object isn't available at that point of execution. Commented Jan 11, 2012 at 19:58
  • Oops, didn't see that was pre-rails. Commented Jan 11, 2012 at 20:36

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.