14

When running:

rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets

Everything precompiles but not my /app/assets/images/*

I even tried adding this to my environment/production.rb

config.assets.paths << "#{Rails.root}/app/assets/images"

What's wrong? thanks!

Kieran Klaassen
  • 2,192
  • 4
  • 21
  • 25
  • Found a solution: Add to `environment/production.rb` config.assets.precompile += %w[*.png *.jp?g *.gif] Why isn't this default ;ike this line says: `(application.js, application.css, and all non-JS/CSS are already added)` – Kieran Klaassen Nov 08 '11 at 16:28
  • *.jp?g doesn't work - the ? matches exactly 1 character as it's a glob match and not a regexp. What you want is `config.assets.precompile += %w[*.png *.jpg *.jpeg *.gif]` – Morgan Christiansson Nov 22 '11 at 11:18
  • Yup, i figured it out, thanks – Kieran Klaassen Nov 27 '11 at 12:28
  • For what it's worth, I thought I was having a similar problem, but after many hours learning a lot more details on how the asset pipeline works, I discovered that I just had a syntax error in the argument passed to my image_tag helper! In Rails 3.2.8 at least, there was no need to change any of the defaults in the environment file. – Jan Hettich Nov 01 '12 at 13:23

3 Answers3

37

Found a solution: Add to environment/production.rb

config.assets.precompile += %w[*.png *.jpg *.jpeg *.gif] 

Why isn't this default ;ike this line says: (application.js, application.css, and all non-JS/CSS are already added)

Kieran Klaassen
  • 2,192
  • 4
  • 21
  • 25
  • *.jp?g doesn't work - the ? matches exactly 1 character as it's a glob match and not a regexp. What you want is `config.assets.precompile += %w[*.png *.jpg *.jpeg *.gif]` – Morgan Christiansson Nov 22 '11 at 12:42
  • Found this was an issue in Rails 4. When we moved our assets directory to /assets instead of /app/assets – David Rice Aug 07 '13 at 19:16
  • 1
    Why the hell we should 'precompile' images? I don't see any sense in doing this – divideByZero Oct 18 '16 at 09:49
  • For anyone else coming here, in Rails 5 it should be `Rails.application.config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif)` in your `config/initializers/assets` folder – ohhh Nov 08 '17 at 15:05
  • @divideByZero e.g. to add the md5 fingerprint, let's say you have some logo.png or default.png images, when you change them, their md5 hash will change as well, and the change in resulting URL to image will force the browser to fetch fresh copy (after change) – januszm Apr 06 '19 at 16:33
2

Use this format for the server:

rails assets:precompile:all -e production
Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
0

In my case some incompatible gem was using something along the lines:

config.assets.precompile << %r(bootstrap-sass/assets/fonts/bootstrap/[\w-]+\.(?:eot|svg|ttf|woff2?)$)

Removing the gem, or updating it could fix the issue. This is because the gem was made for older Rails version.

That wasn't clear directly from the console output.

For my case, as updating the gem wasn't possible at the time being, the solution was to remove the problematic asset path, by adding in application.rb due to compatibility:

config.after_initialize do
    bootstrap_index = config.assets.precompile.index(/bootstrap\/glyphicons-halflings-regular\.(?:eot|svg|ttf|woff2?)$/)
    config.assets.precompile.delete_at(bootstrap_index)
end

And adding missing file that should be precompiled to:

config.assets.precompile += %w( .svg .eot .woff .ttf .woff2)
Aleks
  • 4,866
  • 3
  • 38
  • 69