Rails - Application.CSS Asset Not Found in Production Mode

rails - application.css asset not found in production mode

Rails by default doesn't serve assets under public. See your production.rb:

  config.serve_static_assets = true

Change that to true and you're good to go. (Note: you don't want that to be true in production, remember to change it back before deploying!)

See Configuring Rails Applications for details.

In rails 6, in the default production.rb there should be a line

config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?

So run your server with

RAILS_SERVE_STATIC_FILES=true rails server -e production

or set config.public_file_server.enabled=true in production.rb. See answers below for rails 4 and 5.

Rails 4: assets not loading in production

In /config/environments/production.rb I had to add this:

Rails.application.config.assets.precompile += %w( *.js ^[^_]*.css *.css.erb )

The .js was getting precompiled already, but I added it anyway. The .css and .css.erb apparently don't happen automatically. The ^[^_] excludes partials from being compiled -- it's a regexp.

It's a little frustrating that the docs clearly state that asset pipeline IS enabled by default but doesn't clarify the fact that only applies to javascripts.

Can't load assets ressources (css + js) in production with Rails 6

Seems like you should set RAILS_SERVE_STATIC_FILES environment variable to true

Suggestion is based on this issue comment I found while resolving same trouble

This only affects configuration at config/environments/production.rb:

  # Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?

Rails: assets are missing in production(No route matches [GET] /assets/application-12345.css)

The issue is that in production, assets are precompiled & fingerprinted, hence calling them directly is not going to work. You have to call the filename only:

<%= stylesheet_link_tag :application, media: 'all' %>
<%= javascript_include_tag :application %>

You also need to make sure you keep your stylesheets / javascripts in the appropriate directories. Why you'd put application.css in /javascripts is beyond me.


The reason the above works is because they use the asset_path helper, which allows Rails to pull the appropriate file from a set of relative paths. Rather like the PATH var in operating systems, it means you can call assets which may have been fingerprinted in production.



Related Topics



Leave a reply



Submit