Rails 4.1.6资产管道无法在生产中加载资产和JavaScript [英] Rails 4.1.6 Asset pipeline not loading assets and javascript in production

查看:73
本文介绍了Rails 4.1.6资产管道无法在生产中加载资产和JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个试图在生产中部署的Rails Web服务器上的红宝石.我很难将资产加载到生产中:.css,.js和图像(由于,在开发中似乎工作正常).

I have a ruby on rails web server that I am trying to deploy in production. I am having trouble getting the assets to load in production: .css, .js, and images (seems to work fine in development, due to ).

这是我的作品.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中看起来像这样:

I used to have a deployed version of this same server and its tags looked like this in 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)

in production (loaded 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>

现在部署我的应用程序时,它看起来像这样(没有指纹).预编译似乎无法正常工作.没有在公共/资产中生成的文件,这是一个问题.目前,我的应用程序清单看起来像这样

When my application is deployed now it looks like this (without the fingerprint). The precompile seems to not be working. There are no files generated in public/assets which is a problem. Currently my application manifest looks like this

<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 asset:precompile都不起作用.如何使用滑轨来使用指纹版本?

I believe there is something wrong in regards to the asset pipeline and precompiling assets. It should generate the fingerprint version of the css and js and use them. Even running rake assets:precompile on my production server doesn't work. How do I get rails to use the fingerprint version?

在使用某些设置时,我可以通过更改以下设置使其工作:

In playing around with certain settings I was able to get it to work by changing these settings:

config.assets.compile = true


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

我相信这会大大降低性能,因为我不想在生产中进行编译.我需要纠正解决此问题的方法.任何帮助将不胜感激!

I believe this will drastically slow down performance as I don't want to compile in production. I need to correct way to solve this problem. Any help would be greatly appreciated!

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

Note: I also noticed that I have a application.js in /assets/javascripts but I have a application.css.scss in /assets/stylesheets -- not sure if this would affect it

推荐答案

因此,事实证明这是一个简单的解决方案.我当时正在使用docker推送到生产环境,需要将此行添加到我的Dockerfile中

So it turned out to be a simple solution. I was using docker to push to production and needed to add this line to my Dockerfile

RUN rake assets:precompile

这将对资产进行预编译并添加指纹,以便可以在生产中使用它们.干杯!

This precompiles the assets and adds the fingerprint so they can be served in production. Cheers!

这篇关于Rails 4.1.6资产管道无法在生产中加载资产和JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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