什么时候需要将资产添加到 config.assets.precompile,什么时候不需要?

发布于 01-02 01:35 字数 1356 浏览 3 评论 0原文

我对何时需要向 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

窗影残2025-01-09 01:35:50

预编译的规则很简单:

如果应用程序清单之一中需要某个资产,则无需将其添加到预编译中。

如果需要在 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).

旧情别恋2025-01-09 01:35:50

正如您自己所说,facybox.css 的内容已包含在您的application.css 中。

这意味着您只需从您的部分以及使用 facybox.css 的任何其他位置删除 stylesheet_link_tag 即可。您必须对 application.css 中已包含的所有其他样式表执行相同的操作

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文