CSS不能在Heroku上加载,当我启用的资产服务器(AWS)服务图像?使用Rails4,Carrierwave,Heroku的,亚马逊S3亚马逊、当我、能在、图像

2023-09-11 10:09:11 作者:白衬衫男孩

我在Heroku上的应用程序,并最终设法保存上传的图片(使用Carrierwave)到Amazon S3,它们显示在我的水桶,一切都很好,但我不得不把它添加到我的production.rb:

I have an app on Heroku and finally managed to save uploaded images (using Carrierwave) to Amazon S3, they show in my bucket and all is fine, but I had to add this to my production.rb:

# Enable serving of images, stylesheets, and JavaScripts from an asset server.
config.action_controller.asset_host = "https://s3-us-west-1.amazonaws.com/mybucket"

不过,现在有什么不对我的资产的管道。它看起来从config.action_controller.asset_host的CSS和Javascript,我想正常加载它们在Heroku上。我尝试添加这一点,但它没有工作:

However now there is something wrong with my asset pipeline. It looks for the CSS and Javascript from the config.action_controller.asset_host and I would like to load them normally on Heroku. I tried adding this, but it didn't work:

# Precompile additional assets. application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
  config.assets.precompile += %w( 'custom.css.scss' )

我也想跑RAILS_ENV =产品包的exec耙资产:precompile

I also tried to run RAILS_ENV=production bundle exec rake assets:precompile

在本地主机一切正常,但是当我推到Heroku上,则CSS刹车。

On localhost everything works fine, but when I push to heroku, then the CSS brakes.

在您的帮助非常感谢!

推荐答案

这是一个简单的code直接从的 asset_host

This is a sample code taken directly (but slightly modified) from the documentation of asset_host

ActionController::Base.asset_host = Proc.new { |source|
  if source.ends_with?('.css')
    "http://myapp.herokuapp.com"
  else
    "http://assets.example.com"
  end
}

image_tag("rails.png")
# => <img alt="Rails" src="https://m.xsw88.com/allimgs/daicuo/20230911/3752.png" />
stylesheet_link_tag("application")
# => <link href="http://myapp.herokuapp.com/assets/application.css" media="screen" rel="stylesheet" />