【问题标题】:Rails 3.2 asset_host setting ignoredRails 3.2 资产主机设置被忽略
【发布时间】:2013-02-26 19:25:34
【问题描述】:

我的生产环境-

# 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's 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
   }

但是,当我使用 image_tag 时,它仍然会返回我的 '/assets..' 相对 url,而不是资产主机的绝对 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')

我似乎无法弄清楚可能缺少什么。我什至尝试过简单的 config.asset_host 设置,但它仍然无法识别该设置。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3


    【解决方案1】:

    你试过设置指定的配置吗?

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

    不确定它是否适用于协议相关网址。我只是在我的应用程序中使用 https。

    同样值得注意的是,action mailer 也有类似的设置:

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

    【讨论】:

    • 是的,我也尝试过不使用协议相对 url。它也没有工作。
    • 我的配置文件有问题,导致无法正常工作。将其剥离到最低限度后-asset_host 似乎可以使用 https:// 前缀。所以它不适用于协议相对 URL,但它确实可以使用完整的 URL。谢谢!
    • @Shekhar 是什么问题阻止了它的工作?我有同样的问题。谢谢!
    【解决方案2】:

    Rails 配置中有一些奇怪的地方。根据文档asset_host 应该只包含host 部分('yoursite.com')。我通过助手解决了:

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

    在您的邮件中配置它:

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

    除了将 URL 传递给它之外,没有办法强制 image_tag 使用 URL。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-05
      • 1970-01-01
      • 2018-02-08
      • 1970-01-01
      • 1970-01-01
      • 2012-06-05
      • 2014-07-30
      • 2019-08-07
      相关资源
      最近更新 更多