ruby - Rails 4.1.6 Assets 管道未在生产中加载 Assets 和 javascript

标签 ruby ruby-on-rails-4 asset-pipeline erb production

我有一个 ruby​​ on rails web 服务器,我正试图在生产中部署它。我在生产中加载 Assets 时遇到问题:.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 中没有生成文件,这是一个问题。目前我的应用程序 list 看起来像这样

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

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

在尝试某些设置时,我能够通过更改这些设置使其正常工作:

config.assets.compile = true


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

我相信这会大大降低性能,因为我不想在生产环境中编译。我需要正确的方法来解决这个问题。任何帮助将不胜感激!

注意:我还注意到我在/assets/javascripts 中有一个 application.js,但在/assets/stylesheets 中有一个 application.css.scss —— 不确定这是否会影响它

最佳答案

结果证明这是一个简单的解决方案。我正在使用 docker 推送到生产环境,需要将这一行添加到我的 Dockerfile

RUN rake assets:precompile

这会预编译 Assets 并添加指纹,以便它们可以在生产环境中使用。干杯!

关于ruby - Rails 4.1.6 Assets 管道未在生产中加载 Assets 和 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39752252/

相关文章:

ruby-on-rails - 如何使用 rails 3.2 Assets 管道在 lib/assets 中指定 js 文件的路径

ruby-on-rails - Rails Assets 管道 : how to create custom manifest file

ruby-on-rails - 如何使用 ActionController::Live 以及 Resque + Redis(用于聊天应用程序)

testing - Rails 4 : db:test:prepare says it is deprecated, 但没有它测试无法工作

当查询有派生词时,postgresql 全文搜索找不到任何东西

javascript - 使用 Javascript 读取 Rails 4 Cookie/Session

jquery - 使用 asset pipeline 和 pjax 时未捕获的 ReferenceError

ruby - 如何解码svg数据uri(不是base64)

ruby - 解析来自 API 的字符串

ruby-on-rails - 如何使 Ruby 字符串对文件系统安全?