ruby-on-rails – Rails 4.0.3使用asset_sync生成不正确的资产路径

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – Rails 4.0.3使用asset_sync生成不正确的资产路径前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我以前曾多次使用asset_sync gem取得了巨大成功,但在Rails 4.0.3项目中使用它似乎导致了问题.

资产被上传,散列和gzip到目标目录(我只使用默认的’assets’),但是当在临时/生产环境中运行应用程序时,路径不正确.

他们采取以下形式:

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: "<%= 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'] %>

解决方法

您需要在rails 4中运行与生产模式相关的所有资产.

例如:

rake assets:precompile RAILS_ENV=production

如果您在默认模式(开发)中运行它,则哈希值会有所不同,因此rails会将哈希值全部放在一起.

此外,您需要在启动服务器之前执行此操作,以便找到文件.

注意:我认为此更改是为了允许您在开发中缓存资产.

猜你在找的Ruby相关文章