Invalid Rails 3.2 asset_host parameter

My work environment is

# Code is not reloaded between requests config.cache_classes = true config.assets.enabled = true # Full error reports are disabled and caching is turned on config.consider_all_requests_local = false config.action_controller.perform_caching = true # Disable Rails static asset server (Apache or nginx will already do this) config.serve_static_assets = true config.static_cache_control = "public, max-age=31536000" # Compress JavaScripts and CSS config.assets.compress = true config.assets.js_compressor = :uglifier #config.assets.css_compressor = :yui # Don't fallback to assets pipeline if a precompiled asset is missed config.assets.compile = true # Generate digests for assets URLs config.assets.digest = true # See everything in the log (default is :info) config.log_level = :warn config.log_tags = [:remote_ip, lambda { |req| Time.now }] # Enable serving of images, stylesheets, and JavaScripts from an asset server ActionController::Base.asset_host = Proc.new { |source| unless source.starts_with?('/stylesheets' || '/javascripts') "//dddd.cloudfront.net/" end } 

However, when I use image_tag, it still returns me a '/ assets ..' relative URL, not an absolute host URL.

 irb(main):034:0> helper.image_tag('noimage.gif') => "<img alt=\"Noimage\" src=\"/assets/noimage.gif\" />" irb(main):035:0> helper.image_path('noimage.gif') 

I can’t imagine what might be missing. I even tried a simple setup with config.asset_host, and yet it does not recognize the setup.

+4
source share
2 answers

Did you try to install the designated config?

 config.action_controller.asset_host = 'https://abcd.cloudfront.net' 

Not sure if it works with relative protocol urls. I just use https in my applications.

It may also be useful to note that the action mail program has a similar setting:

 config.action_mailer.asset_host = 'https://abcd.cloudfront.net' 
+3
source

There is something strange about the Rails configuration. According to the documentation, asset_host supposed to host only the host part ('yoursite.com'). I decided with an assistant:

 module EmailsHelper def email_image_tag(src) protocol = ActionMailer::Base.default_url_options[:protocol] || 'http' image_tag asset_path(src, protocol: protocol) end end 

Set it up in your email program:

 class EmailNotificationsMailer < ActionMailer::Base add_template_helper(EmailsHelper) ... end 

There is no way to force image_tag to use a URL other than passing the URL to it.

-1
source

Source: https://habr.com/ru/post/921543/


All Articles