Rails 4.1.6 Asset pipeline not loading assets and

2019-06-27 05:35发布

问题:

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 ).

Here is my 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

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>

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>

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!

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

回答1:

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!