Rails Assets Not Precompiling, CSS Looks Different in Production

Rails assets not precompiling, css looks different in production

In your config/environments/production.rb file, set:

config.serve_static_assets = false (currently it's set to true)

config.assets.compile = true (currently it's set to false)

This should solve your problem.

Let me explain what I am asking you to do.

  • By setting config.serve_static_assets = false, we are telling rails server, don't add ActionDispatch::Static middleware, which is used to serve static assets.

Why not?

That's because in production environment, you are expected to run your application server (like puma) behind a web server (like Apache/Nginx), which is designed to serve static files (including assets) directly, without bothering to send the request to rails application server.

Since, you are not using any web server, we are turning it off.

  • By setting config.assets.compile = true, we are telling rails to compile the requested asset at runtime. i.e. Look for the requested asset in app/assets, vendor/assets, lib/assets and serve it if found from any of these locations.

By default, config.assets.compile is true in development, false in production environment. Since, we are not using web server to serve static assets, we are asking rails to live compile our assets.

For further details refer to the asset pipeline documentation.

Rails Assets Precompile just not working

It looks like it could be that you are add your locally compiled assets to git and pushing them and as a result Heroku is not compiling your assets on push. Check to make sure you are not adding the public/assets directory to git.

Rails 3.1 Asset Pipeline not precompiling files from a different directory

I think the problem is, by adding each site as a path, sprockets only finds the first site.scss

I've tried this using compass, but it should be the same for plain sprockets.
I haven't tried your configurator approach, but it looks straightforward to adapt my arrangement to it.

Can you change your app/sites/* to the more standard file arrangement?

./app/assets/javascripts/application.js
./app/assets/stylesheets/screen.css.scss
./app/assets/stylesheets/othersite/screen.css.scss

Change your config/application.rb, and add each of your sites.
This will pregenerate all styles, on each of your hosts:

config.assets.precompile += ['screen.css', 'othersite/screen.css']

In your view/layouts/application, you'll need to configure the path to sitename:

= stylesheet_link_tag '[your sitename here]/screen'

After I rake assets:clean and precompile, I see this in public:

./assets/othersite/screen.css
./assets/othersite/screen.css.gz
./assets/screen.css
./assets/screen.css.gz

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.



Related Topics



Leave a reply



Submit