什么时候需要将资产添加到 config.assets.precompile,什么时候不需要?
我对何时需要向 config.assets.precompile 添加资产以及何时不需要添加资产感到有点困惑。
(我的问题可能源于这个应用程序是从 Rails 2.x 迁移而来的;很快的某个时候,我将把它从头开始重建为 3.x 应用程序,但没有)
这是我的问题:我有一个 .css
和 .js
文件,除非我将它们添加到,否则 sprocket 找不到它们application.rb
中的 config.assets.precompile
。我无法想象我必须对每个 .js
和 .css
执行此操作,是吗?
例如,我遇到此问题的一个文件是 app/assets/stylesheets/facybox.css
。
application.css
是:(
/*
*= require_self
*= require_directory .
*/
是的,故意使用 require_directory
而不是 require_tree
)。
我在部署期间在我的服务器上运行 rake asset:precompile
。生成的 application.css
中包含 facybox.css
的内容。
facybox.css
在部分中被引用,如下所示:
<% content_for :header do %>
<%= stylesheet_link_tag "facybox" %>
<% end %>
但是当我转到包含部分的页面时,我得到:
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
我认为这是因为 sprockets 正在寻找文件的指纹副本,它不存在除非我将其添加到config.assets.precompile
。那么一切就都很顺利了。
有人可以解释一下吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

预编译的规则很简单:
如果应用程序清单之一中需要某个资产,则无需将其添加到预编译中。
如果需要在 Rails 视图中直接引用资产,那么您确实需要添加它(并且应该从任何清单中将其删除)。
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).