I have some settings I need in a Javascript file — servers to connect to — that changes based on my environment. For development, test, and staging, I want to use the staging servers; for production, the production servers. I already have the settings in Ruby (configured in my environment/xyz.rb files). So far, I’ve been dynamically creating the JS files at request time with an application.js.erb file and a custom route. This is pretty slow, though, and it means creating an extra controller and views directory just for this file.
I would prefer to have a template file and a rake task that generates the correct version from the template and places a static file in the public/javascripts directory. Has anyone tried something like this? What did you use for rendering? Where did you put the template file and the rendering code?
Or is it better to just keep the dynamic version and cache it in production?
Can you do something like this?
I do something similar, though it is not with a JS file, but with some RB files … and I do it for the same reason, determining which servers to connect to.
For me, I have an rb file in my
'lib'directory called'constants.rb'. This file has a few if statements that switch based on environment (development, test, staging, or production) that switch out the values of the ip addresses that I need to attach to. It is peppy enough for my purposes on the behemoth of a machine that I have the application running on.Here is a sample: