Rails 3.1 Asset pipeline - Why my images do not precompile for production?
Asked Answered
O

3

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!

Occultism answered 8/11, 2011 at 15:30 Comment(4)
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)Occultism
*.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]Casiano
Yup, i figured it out, thanksOccultism
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.Chadwickchae
O
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)

Occultism answered 11/11, 2011 at 22:0 Comment(5)
*.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]Casiano
Found this was an issue in Rails 4. When we moved our assets directory to /assets instead of /app/assetsKerplunk
Why the hell we should 'precompile' images? I don't see any sense in doing thisYap
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 folderRapping
@Yap 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)Anthropocentric
B
2

Use this format for the server:

rails assets:precompile:all -e production
Basel answered 8/11, 2011 at 16:5 Comment(4)
you mean rake assets:precompile:all ?Occultism
Try `rails assets:precompile -e production'Basel
don't need to specify environment with assets:precompile it's production by defaultUnconcern
A quick note: rake assets:precompile:all doesn't generate the hashed (fingerprinted) versions of my assets. For that reason, I returned to using just rake assets:precompileCherlycherlyn
K
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)
Kalevala answered 29/12, 2021 at 17:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.