denishaskin
denishaskin

Reputation: 3425

when do I need to add an asset to config.assets.precompile and when not?

I'm a little confused by when I need to add an asset to config.assets.precompile and when it's not necessary.

(It's possible that my problem may stem from the fact that this app was migrated from rails 2.x; at some point soon I'm going to rebuild it from the ground up as a 3.x app, but don't have the time for that yet.)

Here's my issue: I have a .css and .js files that are not being found by sprockets unless I add them to config.assets.precompile in application.rb. I can't imagine I have to do this for every .js and .css, do I?

For example, one file I'm having this issue with is app/assets/stylesheets/facybox.css.

application.css is:

/*
 *= require_self
 *= require_directory .
*/

(yes, require_directory instead of require_tree intentionally).

I run rake assets:precompile on my server during deploy. The resulting application.css has the contents of facybox.css in it.

facybox.css is referred to in a partial, like this:

<% content_for :header do %>
  <%= stylesheet_link_tag "facybox" %>
<% end %>

But when I go to a page that includes the partial, I get:

Sprockets::Helpers::RailsHelper::AssetPaths::AssetNotPrecompiledError in Admin#compositions
Showing /srv/zmx/releases/5420c4dde6fbec53d78cffe78396085f263ed039/app/views/shared/_preview_assets.erb where line #6 raised:
facybox.css isn't precompiled

which I assume is because sprockets is looking for the fingerprinted copy of the file, which doesn't exist unless I add it to config.assets.precompile. Then all is hunky-dory.

Can someone explain?

Upvotes: 1

Views: 242

Answers (2)

Richard Hulse
Richard Hulse

Reputation: 10493

The rules for precompiling are simple:

If an asset is required in one of the application manifests you do not need to add it to precompile.

If an asset needs to be referenced directly in a Rails view then you DO need to add it (and you should remove it from any manifests).

Upvotes: 2

Wolfgang
Wolfgang

Reputation: 4925

As you said yourself, the contents of facybox.css are already included in your application.css.

This means that you can just remove the stylesheet_link_tag from your partial and also any other places where you use facybox.css. You would have to do the same for all other stylesheets that you already have included in your application.css

Upvotes: 1

Related Questions