17

I have some images (svg) in my app/assets/images folder. According to the Rails Guides, all the files in the assets folder should be automatically precompiled.

However, when I reference the the image using image_tag('filename'), it shows me an Sprockets::Rails::Helper::AssetNotPrecompiled error

Asset was not declared to be precompiled in production.

It tells me to declare the file to be precompiled manually, but why should that be necessary? On top of that, why does it concern itself with the production environment when I am doing everything in development?

3 Answers 3

17

If you added the image after you've started the server in development, restart the server. Sprockets will then precompile that image and the error will go away.

Sign up to request clarification or add additional context in comments.

3 Comments

This was my issue; restarting the server fixed it.
@WardBekker The answer I marked as accepted actually was the one which accurately described the problem and it's solution. Tim's answer though being a valid one for a lot of people (as is seen by the comments) wasn't one for me.
Restarting the server does absolutely nothing. Is this an answer to a different question?
7

I'm pretty sure Rails doesn't support .svg yet, hence why it would ignore it.

You'll need to include the file extensions in your config/application.rb file:

#config/application.rb
config.assets.precompile += %w(.svg)

In regards the application concerning itself with the production environment, you have to remember that the precompilation process is meant for production:

The first feature of the pipeline is to concatenate assets, which can reduce the number of requests that a browser makes to render a web page. Web browsers are limited in the number of requests that they can make in parallel, so fewer requests can mean faster loading for your application.

Concantenating assets essentially means to compile your asset files into a single file, which is typically then minified.

--

Although this can be done in real-time, it's mostly the realm of static assets (which have to be precompiled). This means that if you run the rake asstes:precompile task, it will work on the development environment, unless you call RAILS_ENV=production rake assets:precompile (which sets it to the production environment for that request.

why does it concern itself with the production environment when I am doing everything in development

The application is going to run in production, not development.

Ultimately, everything you do in development should make it easier / better to work in production. In the sense of your assets, it means that you can use many of the quirks of Rails' asset pipeline, from sprockets to preprocessors such as SASS & Coffeescript

6 Comments

No. It is not just svg. Even a png image in app/assets/images folder cant be found by rails. I am unable to include any images at all. I don't understand why. I understand how the asset pipeline works, but it seems very weird to me that Rails wouldn't include the assets despite declaring that all non-css and non-js assets are automatically included if they are in the assets folder.
Thanks for the clarification, let me have a look at it
Well, I made a few changes, and it worked. I don't know which one of the the changes did the trick, but it might have been removing dashes(-) from the file name. Thanks for the answer, even though it did not solve the problem.
No problem, would you rather I delete it?
I have a similar problem. I have no - in my png file name. Curiously, the assets can be accessed just fine when referenced from the SCSS, just not via image_tag.
|
5

It's probably because you didn't specify the complete image name. I ran into this problem after updating the gem too. Before I just used image_tag 'some-image', but it seems that you now have to specify what type of image/extension you want to use.

Try this: image_tag 'some-image.svg'. It worked for me.

Cheers.

2 Comments

Just reproduced the issue to verify. And your solution seems to be the correct one. That was indeed very silly of me. Thanks!
I have the same problem eventhough I put the whole file name

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.