Rails 4.0.3使用asset_sync生成不正确的资产路径 [英] Rails 4.0.3 generating incorrect asset paths with asset_sync

查看:111
本文介绍了Rails 4.0.3使用asset_sync生成不正确的资产路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前,我已经多次使用asset_sync gem取得了很大的成功,但是在Rails 4.0.3项目中使用它似乎引起了问题.

I have used the asset_sync gem many times before with great success, but using it in a Rails 4.0.3 project seems to have caused a problem.

资产已上传,哈希化并压缩到目标目录(我只是使用默认的资产"),但是在暂存/生产环境中运行应用程序时,路径不正确.

The assets are uploaded, hashed and gzipped to the target directory (I just went with the default 'assets'), but when running the application in staging/production environment the paths are incorrect.

它们采用以下形式:

S3_DOMAIN.com/stylesheets/application.css

代替:

S3_DOMAIN.com/assets/application-HASH.css

其他人遇到过这个问题吗?我发现扭转这种现象的唯一方法是将config.assets.compile设置为true,但这在生产环境中不会实现.

Has anyone else experienced this problem? The only way I have found to reverse this behaviour is to set config.assets.compile to true, but this won't do in a production environment.

以下是相关的配置文件:

Here are the relevant config files:

  ## environments/staging.rb
  config.serve_static_assets = false
  config.assets.compress = true
  config.assets.js_compressor = :uglifier
  config.assets.css_compressor = :sass
  # Have to set this to true to make asset_sync generate the correct links
  config.assets.compile = false
  config.assets.digest = true
  config.assets.enabled = true
  config.assets.initialize_on_precompile = true
  config.action_controller.asset_host = "//#{Figaro.env.fog_directory}.s3.amazonaws.com"
  config.action_mailer.asset_host = "//#{Figaro.env.fog_directory}.s3.amazonaws.com"
  config.assets.prefix = "/assets"
  config.assets.debug = false
  config.assets.cache_store = :memory_store

##config/asset_sync.yml
defaults: &defaults
  fog_provider: 'AWS'
  aws_access_key_id: "<%= ENV['AWS_ACCESS_KEY_ID'] %>"
  aws_secret_access_key: "<%= ENV['AWS_SECRET_ACCESS_KEY'] %>"
  # To use AWS reduced redundancy storage.
  # aws_reduced_redundancy: true
  # You may need to specify what region your storage bucket is in
  fog_region: <%= ENV['FOG_REGION'] %>
  existing_remote_files: keep
  # To delete existing remote files.
  # existing_remote_files: delete
  # Automatically replace files with their equivalent gzip compressed version
  gzip_compression: true
  # Fail silently.  Useful for environments such as Heroku
  # fail_silently: true

development:
  <<: *defaults
  enabled: false

test:
  <<: *defaults
  enabled: false

staging:
  <<: *defaults
  fog_directory: <%= ENV['FOG_DIRECTORY'] %>

production:
  <<: *defaults
  fog_directory: <%= ENV['FOG_DIRECTORY'] %>

推荐答案

希望这可以帮助我的程序员朋友节省一些时间:D.我已经在" digest_path& asset_digest_path不允许使用摘要网址",但会在此处重新发布摘要网址,这样可以为您节省一些点击次数.

Hopefully this will help save my fellow programmer friends some head banging :D. I have answered this question on "digest_path & asset_digest_path not allowing digest URLs" but will repost it here so it will save you some clicks.

我正在将文件上传到S3,但我没有意识到清单不是由Rails加载的.您可以正确设置所有生产设置(如上面和其他线程中一样),但是如果您没有Rails可读的manifest.json文件,它将仍然生成/javascript/*(示例)URL.

I was uploading the files to S3, I didn't realize that the manifest wasn't loaded by Rails. You can have all your production settings right (like above and in other threads), but if you don't have the manifest.json file readable by Rails, it will still generate /javascript/* (example) urls.

我仍然无法使用multi_json gem的最新版本,因此我将其降级为1.7.8,并且可以正常工作.

I was still having trouble with multi_json gem's latest version, so I downgraded it to 1.7.8 and it works fine.

gem 'multi_json', '1.7.8'

因此,它可以读取rake assets:precompile创建的manifest.json文件.

That's so it can read the manifest.json file which rake assets:precompile creates.

对此链轮线程有一个辩论 https://github.com/rails/sprockets-rails/issues/107 关于清单文件是应该在git中还是仅在部署脚本中,请执行最适合您的事情,只需确保可在以下位置找到该文件:

There is a debate on this sprockets thread https://github.com/rails/sprockets-rails/issues/107 on whether your manifest file should be in git or just on a deploy script, do what suits you best, just make sure it is findable in:

/public/assets/manifest.json 

或自行指定

config.assets.manifest = '...'

可以或可以不使用它.

干杯!

这篇关于Rails 4.0.3使用asset_sync生成不正确的资产路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆