Rails资产管道-image_path帮助器仅在开发中工作 [英] Rails asset pipeline - image_path helper only working in development

查看:45
本文介绍了Rails资产管道-image_path帮助器仅在开发中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Rails(3.2.7)中遇到了资产预编译问题.

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

我包括一个这样的图标:

I am including an favicon like this:

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

在开发模式下,我设置为config.assets.compile = true.一切正常,呈现的HTML如下所示:

On development mode I set config.assets.compile = true. There everything works fine, the rendered HTML looks like this:

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

但是在生产环境中,我在其中设置了config.assets.compile = false时出现了错误

But on production, where I set config.assets.compile = false, I get the error

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

我已经运行过rake assets:precompile,并且我可以清楚地看到该资产在public/assets/favicon.png下可用.

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

我知道,我可以在生产中设置config.assets.compile = true,但是我不想这样做(由于性能原因).

I know, that i could set config.assets.compile = true in production, but i don´t want to do that (because of performance reasons).

有人知道为什么我的Rails应用程序无法解析生产中资产的正确路径吗?谢谢!

Has anyone an idea, why my rails app is not able to resolve the correct path to the asset in production? Thanks!

更新: 也许也很有用:它不仅会发生在图像上,还会发生在其他资产上.

Update: maybe also useful to know: It happens not only for images, also for other assets.

例如,当config.assets.compile设置为false时,<%= stylesheet_link_tag "screen", :media => "all" %>也会产生错误screen.css isn't precompiled.

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

推荐答案

好吧,在我尝试了几次之后,如何解决这个问题.但是,这有点奇怪,不能完全满足我的要求.仅当我将digest设置为true并提供manifest的路径时,它才对我有用:

Ok, after a couple of tries I figured out, how to fix this. Nevertheless it´s a little bit strange and does not satisfy me completely. It only worked for me, when I set digest to true and provide the path to the manifest:

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

知道这个逻辑"的背后是什么会很有趣.

It would be interesting to know, what´s behind this "logic".

这篇关于Rails资产管道-image_path帮助器仅在开发中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆