简体   繁体   中英

Rails not precompiling images in the app/assets/images folder?

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?

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM