superluminary superluminary - 4 months ago 18
Ruby Question

Is there a way to set set config.action_controller.asset_host in development

In my production.rb I set my asset_host to CloudFront like so:

config.action_controller.asset_host = 'http://xxxxxxxx.cloudfront.net'


Now I'm finding that in some circumstances (specifically, outputting JavaScript to be embedded into another site) I need to set the asset_host in the development environment too, the default null won't cut it. Ideally I want to set:

config.action_controller.asset_host = 'http://localhost:3000'


but this port can't be guaranteed, and I'm reluctant to hard-code it. Is there a way to set asset_host to the current domain and port?

Thanks!

Answer

You can make use of environment variables or Rails initializer parameters

config.action_controller.asset_host = ENV[ASSET_HOST].empty? ? 'http://' + Rails::Server.new.options[:Host] + ':' + Rails::Server.new.options[:Port] : ENV[ASSET_HOST]

This way if you set the environment variable you use that address otherwise it will use the default.

Comments