Rails 4.1.6资产管道在生产环境中无法加载资产和JavaScript

4

我有一个基于ruby on rails的web服务器,我正在尝试将其部署到生产环境中。我遇到了在生产环境中加载静态资源(.css、.js和图片)的问题(似乎在开发环境中运行良好,原因不明)。

这是我的production.rb配置文件:

Rails.application.configure do

  # Code is not reloaded between requests.
  config.cache_classes = true

  # Eager load code on boot. This eager loads most of Rails and
  # your application in memory, allowing both threaded web servers
  # and those relying on copy on write to perform better.
  # Rake tasks automatically ignore this option for performance.
  config.eager_load = true

  # Full error reports are disabled and caching is turned on.
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true

  config.serve_static_files = true

  # Compress JavaScripts and CSS.
  config.assets.js_compressor = :uglifier

  # Do not fallback to assets pipeline if a precompiled asset is missed.
  config.assets.compile = false

  # Generate digests for assets URLs.
  config.assets.digest = true
end

我曾经有一个部署好的同样的服务器,它在application.html.erb中的标签如下所示:

<head>
  <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>
</head>

正在生产环境中(已加载css/js)

<link data-turbolinks-track="true" href="/assets/application-06ed3643d0bf74fdf192f533cc269506.css" media="all" rel="stylesheet" />
<script data-turbolinks-track="true" src="/assets/application-f134ff018deb0477bd5ad046d853559d.js"></script>

当我的应用程序现在部署时,它看起来像这样(没有指纹)。预编译似乎不起作用。在public/assets中没有生成任何文件,这是一个问题。目前我的应用程序清单如下:

<link data-turbolinks-track="true" href="/stylesheets/application.css" media="all" rel="stylesheet" />
<script data-turbolinks-track="true" src="/javascripts/application.js"></script>

我认为与资产管道和预编译资产有关的问题出现了一些错误。它应该生成 css 和 js 的指纹版本并使用它们。即使在我的生产服务器上运行 rake assets:precompile 也不起作用。如何让 Rails 使用指纹版本?

通过更改以下设置,我成功地进行了某些设置调整:

config.assets.compile = true


Rails.application.config.assets.precompile << /(^[^_\/]|\/[^_])[^\/]*$/

我认为这样做会严重影响性能,因为我不想在生产环境中进行编译。我需要正确的解决方法。非常感谢您的帮助!
注意:我还注意到/assets/javascripts中有一个application.js文件,但是在/assets/stylesheets中有一个application.css.scss文件——不确定是否会受到影响。
1个回答

1
所以事实证明这是一个简单的解决方案。我正在使用Docker推送到生产环境,需要在我的Dockerfile中添加这一行。
RUN rake assets:precompile

这将预编译资源并添加指纹,以便在生产环境中提供服务。干杯!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接