Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 484
Pawan Avatar asked Dec 26 '15 13:12

Pawan


People also ask

How to add images to a Rails application?

Images are an integral part of every application, and this is why we are going to learn how to add images to a rails application. To do this, go to app/assets/images and add all your file to the images folder.

Where are the assets in the rails asset pipeline?

In previous versions of Rails, all assets were located in subdirectories of public such as images, javascripts and stylesheets. With the asset pipeline, the preferred location for these assets is now the app/assets directory. Files in this directory are served by the Sprockets middleware. Assets can still be placed in the public hierarchy.

What files does rails generate when generating a scaffold?

Additionally, when generating a scaffold, Rails generates the file scaffolds.css (or scaffolds.scss if sass-rails is in the Gemfile .) For example, if you generate a ProjectsController, Rails will also add a new file at app/assets/stylesheets/projects.scss.

How are rails assets served to the server?

By default Rails assumes assets have been precompiled and will be served as static assets by your web server. During the precompilation phase an SHA256 is generated from the contents of the compiled files, and inserted into the filenames as they are written to disk.


2 Answers

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.

like image 106
Tim B. Avatar answered Oct 17 '22 08:10

Tim B.


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

like image 45
Richard Peck Avatar answered Oct 17 '22 06:10

Richard Peck