Rails 4.0.3使用asset_sync生成不正确的资产路径
我之前已多次使用asset_sync gem取得了巨大成功,但在Rails 4.0.3项目中使用它似乎导致了问题。
资产被上传,散列和gzip到目标目录(我只是使用默认的’assets’),但是当在临时/生产环境中运行应用程序时,路径是不正确的。
他们采取以下forms:
S3_DOMAIN.com/stylesheets/application.css
代替:
S3_DOMAIN.com/assets/application-HASH.css
还有其他人遇到过这个问题吗? 我发现反转此行为的唯一方法是将config.assets.compile设置为true,但这在生产环境中不起作用。
以下是相关的配置文件:
## 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: "" 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: 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: production: <<: *defaults fog_directory:
您需要在rails 4中运行production
模式中的所有相关资产。
例如:
rake assets:precompile RAILS_ENV=production
如果您在默认模式(开发)中运行它,则哈希值会有所不同,因此rails会将哈希值全部放在一起。
此外,您需要在启动服务器之前执行此操作,以便找到文件。
注意:我认为此更改是为了允许您在开发中缓存资产。
希望这将有助于拯救我的同事程序员朋友一些头撞:D。 我已在“ digest_path&asset_digest_path不允许摘要url ”上回答了这个问题,但会在此处重新发布,以便为您节省一些点击。
我正在将文件上传到S3,我没有意识到Rails没有加载清单。 您可以正确设置所有生产设置(如上面和其他线程),但如果您没有Rails可读的manifest.json文件,它仍会生成/ javascript / *(示例)URL。
我在使用multi_json
gem的最新版本时仍然遇到问题,因此我将其降级为1.7.8并且工作正常。
gem 'multi_json', '1.7.8'
这样就可以读取rake assets:precompile
创建的manifest.json
文件。
关于这个链接线程https://github.com/rails/sprockets-rails/issues/107关于你的清单文件应该是git还是只是在部署脚本上有争议,做最适合你的事情,只需确保它可以在:
/public/assets/manifest.json
或者自己指定
config.assets.manifest = '...'
这可能会也可能不会被贬低。
干杯!