简体   繁体   English

如何使用Assets Sync gem在Rails应用程序上引用S3图像?

[英]How to refence S3 images on rails app with the Assets Sync gem?

I'm using the Assets Sync to upload the assets to the S3 on deploy. 我正在使用Assets Sync将资产上传到部署时的S3。 All the assets are uploaded correctly and stored on S3. 所有资产均已正确上传并存储在S3上。

The js, css, fonts used in the app are pointing to the S3, except the images 应用程序中使用的js,css,字体均指向S3,但图像除外

I guess the problem may be on the rails app... 我想问题可能出在应用程序上...

What is going on? 到底是怎么回事?

My initializer config file: 我的初始化程序配置文件:

AssetSync.configure do |config|
  config.fog_provider = 'AWS'
  config.fog_directory = ENV['FOG_DIRECTORY']
  config.aws_access_key_id = ENV['AWS_ACCESS_KEY_ID']
  config.aws_secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']

  # Don't delete files from the store
  # config.existing_remote_files = 'keep'
  #
  # Increase upload performance by configuring your region
  # config.fog_region = 'eu-west-1'
  #
  # Automatically replace files with their equivalent gzip compressed version
  config.gzip_compression = true
  #
  # Use the Rails generated 'manifest.yml' file to produce the list of files to
  # upload instead of searching the assets directory.
  # config.manifest = true
  #
  # Fail silently.  Useful for environments such as Heroku
  config.fail_silently = true
end

config/enviroment/staging.rb 配置/环境/staging.rb

MyApp::Application.configure do
  routes.default_url_options[:host]= ENV['DOMAIN']

  config.cache_classes = true  
  config.cache_store = :dalli_store
  config.static_cache_control = "public, max-age=2592000"
  config.action_controller.perform_caching = true

  config.consider_all_requests_local = false

  config.serve_static_assets = true
  config.assets.compress = true
  config.assets.compile = false
  config.assets.digest = true
  config.assets.enabled = true
  config.assets.js_compressor = :uglifier
  config.assets.precompile += %w(static.js static.css vendor.js)

  config.assets.paths << Rails.root.join('app', 'assets', 'fonts')

  config.action_controller.asset_host = "//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"
  config.assets.prefix = "/assets"

  config.i18n.fallbacks = true
  config.active_support.deprecation = :notify

end

Thanks! 谢谢!

The problem is with your CSS 问题出在你的CSS

We just had to fix this issue, and it's pretty simple if you know what's up 我们只需要解决此问题,如果您知道发生了什么,这非常简单

Your app needs to use SCSS (SASS) , to ensure that rake can compile the image_paths into your CSS files. 您的应用程序需要使用SCSS(SASS) ,以确保rake可以将image_paths编译为CSS文件。

So instead of using 所以不要使用

/*-- Typical .CSS (static) --*/
.profile_page {
    background: url(background.png);
}

You'll need to make sure that your app can handle the compilation process, which is where SCSS comes in: 您需要确保您的应用程序可以处理编译过程,这是SCSS的来源:

/*-- file.css.scss (dynamic) --*/
.profile_page {
    background: asset_url(background.png)
}

There is a great resource about SCSS in rails apps here: Proper SCSS Asset Structure in Rails 这里有关于 Rails应用程序中SCSS的大量资源Rails中正确的SCSS资产结构

What we've done is change application.css -> application.css.scss , then imported various different .scss files with the asset_path definition inside: 我们要做的是更改application.css > application.css.scss ,然后导入其中带有asset_path定义的各种不同的.scss文件:

@import 'fonts';

This means that we can change the fonts.css.scss file to have this format: 这意味着我们可以将fonts.css.scss文件更改为具有以下格式:

/*-- Akagi Font --*/
@font-face {
    font-family: 'akagi';
    src: asset_url('fonts/akagi-th-webfont.eot');
    src: asset_url('fonts/akagi-th-webfont.eot?#iefix') format('embedded-opentype'),
         asset_url('fonts/akagi-th-webfont.woff') format('woff'),
         asset_url('fonts/akagi-th-webfont.ttf') format('truetype'),
         asset_url('fonts/akagi-th-webfont.svg#akagithin') format('svg');
    font-weight: 300;
    font-style: normal;
}
@font-face {
    font-family: 'akagi';
    src: asset_url('fonts/akagi-bk-webfont.eot');
    src: asset_url('fonts/akagi-bk-webfont.eot?#iefix') format('embedded-opentype'),
         asset_url('fonts/akagi-bk-webfont.woff') format('woff'),
         asset_url('fonts/akagi-bk-webfont.ttf') format('truetype'),
         asset_url('fonts/akagi-bk-webfont.svg#akagibook') format('svg');
    font-weight: 400;
    font-style: normal;
}
@font-face {
    font-family: 'akagi';
    src: asset_url('fonts/akagi-sb-webfont.eot');
    src: asset_url('fonts/akagi-sb-webfont.eot?#iefix') format('embedded-opentype'),
         asset_url('fonts/akagi-sb-webfont.woff') format('woff'),
         asset_url('fonts/akagi-sb-webfont.ttf') format('truetype'),
         asset_url('fonts/akagi-sb-webfont.svg#akagisemibold') format('svg');
    font-weight: 500;
    font-style: normal;
}

This fixes all the paths, but makes rake asset:precompile realllllllly slow 这样可以修复所有路径,但会使rake asset:precompile实际上变慢


Important Note 重要的提示

There is a trick to getting this to work correctly, and it's to precompile the assets in the production environment, like this: 有一个技巧可以使其正常工作,并且可以在生产环境中预编译资产,如下所示:

rake assets:precompile RAILS_ENV=production

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM