如何在Rails 4中指定asset_host?

3

我正在使用Amazon S3进行文件上传,并使用Cloudfront提供静态资源。在我的config/environments/staging.rb文件中,我写了以下内容:

ActionController::Base.asset_host = Proc.new { |source|
  if source.include?('/assets')
    "d1xw0c7m8has5k.cloudfront.net"
  else
    "#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"
  end
}

ActionMailer::Base.asset_host = Proc.new { |source|
  if source.include?('/assets')
    "d1xw0c7m8has5k.cloudfront.net"
  else
    "#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"
  end
}

但是在我的Rails应用程序发送的邮件中,我看到src属性为空的image_tag?也就是说,没有显示图像。我无法确定这个设置有什么问题,请帮忙。

1个回答

10

我通过将这些配置放置在config/environments/staging.rb文件的块中解决了这个问题。

Demo::Application.configure do
  config.action_controller.asset_host = Proc.new { |source|
    if source.include?('/assets')
      "d1xw0c7m8has5k.cloudfront.net"
    else
      "#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"
    end
  }

  config.action_mailer.asset_host = Proc.new { |source|
    if source.include?('/assets')
      "http://d1xw0c7m8has5k.cloudfront.net"
    else
      "http://#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"
    end
  }

end

必须为action_mailer asset_host提供协议,即 'HTTP'


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接