Rails resource pipeline - image_path helper only works in development

I have a problem with asset precompilation in Rails (3.2.7).

I turn on the following icon:

<link rel="icon" type="image/png" href="<%= image_path("favicon.png") %>" /> 

In development mode, I set config.assets.compile = true . Everything works fine there, the displayed HTML looks like this:

 <link rel="icon" type="image/png" href="/assets/favicon.png" /> 

But when creating, where I set config.assets.compile = false , I get an error

 Sprockets::Helpers::RailsHelper::AssetPaths::AssetNotPrecompiledError in Home#index ... favicon.png isn't precompiled 

I have already done rake assets:precompile , and I clearly see that the asset is available under public/assets/favicon.png .

I know that I could set config.assets.compile = true to production, but I don't want to do this (due to performance reasons).

Does anyone have an idea why my rails application is not able to resolve the correct path to an asset in production? Thanks!


Update: it may also be useful to know: this happens not only for images, but also for other assets.

For example, <%= stylesheet_link_tag "screen", :media => "all" %> screen.css isn't precompiled <%= stylesheet_link_tag "screen", :media => "all" %> also generates a screen.css isn't precompiled when config.assets.compile is set to false.

+4
source share
2 answers

Well, after several attempts, I figured out how to fix it. However, this is a little strange and does not completely satisfy me. It worked only for me when I set digest to true and provided the path to manifest :

 config.assets.compile = false config.assets.digest = true config.assets.manifest = Rails.root.join("public/assets") 

It would be interesting to know what is behind this "logic."

+1
source

You must tell Rails which assets should be precompiled. You do this in config/application.rb or config/environments/production.rb using the config.assets.precompile configuration file .

Rails starts with the default list for precompilation, including ["application.js", "application.css"] , but if you want your own assets to be precompiled as well, you must add them to the list.

For instance:

 # config/application.rb module MyApp class Application < Rails::Application # other config ... config.assets.precompile += ["screen.css", "*.png"] end end 
0
source

Source: https://habr.com/ru/post/1433561/


All Articles