Glad You're Ready. Let's Get Started!

Let us know how we can contact you.

Thank you!

We'll respond shortly.

Getting Compass to put generated sprites somewhere reasonable

When generating sprites with Compass (which is extremely easy), we found that the default output directory for your sprites is the same as the root images directory. This is annoying because we would have to add a line line app/assets/images/icons-*.png to our .gitignore, repeated for each sprite file.

We wanted to put all our sprites in a single folder to be put in .gitignore — this was easy to find, by adding config.compass.generated_images_dir = 'public/sprites' to our config/application.rb. The next problem was that while the sprite file was correctly being saved to e.g. public/sprites/icons-xxx.png, the client-facing path to the sprite file was still /assets/icons-xxx.png which was always 404ing.

The final answer came from an open pull request on compass-rails which clearly explains that you need to add the output path to the assets path, e.g. config.assets.paths << Rails.root.join('public', 'sprites'). Finally, we can easily add public/sprites to our .gitignore.

The other gotcha we encountered today is that compass-rails only regenerates the sprite file when the sprites CSS file changes, not when you add or remove files from the globbed path.

  1. Josh says:

    Thank you, but this leads to an error “undefined method `compass`” in config/application.rb! I guess this is because you have your compass gem not nested in the :assets group, but I have.

  2. Philip says:

    If anyone has come here looking for a grunt solution, the key is to set ‘generatedImagesDir’ and ‘httpGeneratedImagesPath’ in your Gruntfile.

    This stackoverflow post provides an outline:

Post a Comment

Your Information (Name required. Email address will not be displayed with comment.)

* Copy This Password *

* Type Or Paste Password Here *

Share This