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

In my production.rb, I set my resource for CloudFront as follows:

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

Now I have found that in some cases (in particular, JavaScript output must be embedded in another site) I also need to set asset_host in the development environment, a default value of zero will not reduce it. Ideally, I want to install:

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

but this port cannot be guaranteed, and I don’t want to hard code it. Is there a way to set asset_host for the current domain and port?

Thanks!

+7
source share
4 answers

You can use environment variables or Rails initialization parameters

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

Thus, if you set the environment variable, you use this address, otherwise it will use the default value.

+5
source

This value is available at startup time and may help:

 Rails::Server.new.options[:Port] 

Try adding it to the asset_host variable of your development.rb file.

Based on this answer: stack overflow

+3
source

In Rails 4, we use the active_host dynamic configuration with proc:

 # in /config/environments/development.rb Rails.application.configure do config.action_controller.asset_host = Proc.new { |source, request| # source = "/assets/brands/stockholm_logo_horizontal.png" # request = A full-fledged ActionDispatch::Request instance # sometimes request is nil and everything breaks scheme = request.try(:scheme).presence || "http" host = request.try(:host).presence || "localhost:3000" port = request.try(:port).presence || nil ["#{scheme}://#{host}", port].reject(&:blank?).join(":") } # more config end 

This code ensures that requests from localhost: 3000, localhost: 8080, 127.0.0.1lla000, local.dev and any other settings will work.

+1
source

Try:

 class ApplicationController < ActionController::Base before_filter :find_asset_host private def find_asset_host ActionController::Base.asset_host = Proc.new { |source| if Rails.env.development? "http://localhost:3000" else {} end } end 
0
source

All Articles