【问题标题】:Rails asset pipeline - image_path helper only working in developmentRails 资产管道 - 仅在开发中工作的 image_path 助手
【发布时间】:2012-09-11 10:49:56
【问题描述】:

我在 Rails (3.2.7) 中的资产预编译存在问题。

我包含一个这样的网站图标:

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

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

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

但是在生产中,我设置了config.assets.compile = false,我得到了错误

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

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

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

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


更新: 了解可能也很有用:它不仅适用于图像,也适用于其他资产。

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

【问题讨论】:

  • 我正在使用公用文件夹来存储图像,因为它在任何环境中都不适合我。

标签: ruby-on-rails-3 asset-pipeline


【解决方案1】:

你必须告诉 Rails 哪些资产要被预编译。您可以在 config/application.rbconfig/environments/production.rb 中使用 config.assets.precompile config key 执行此操作。

Rails 从一个默认的预编译资产列表开始,包括 ["application.js", "application.css"],但如果您也希望自己的资产也被预编译,则必须将它们添加到列表中。

例如:

# config/application.rb
module MyApp
  class Application < Rails::Application
    # other config ...

    config.assets.precompile += ["screen.css", "*.png"]
  end
end

【讨论】:

  • 感谢您的回答。但我已经在我的 application.rb 中配置了额外的资产:config.assets.precompile += ["admin.css", "screen.css", "static.css", "*.png"]。所有需要的文件也都列在 manifest.yml 中,可以在 /public/assets 文件夹中找到
【解决方案2】:

好的,经过几次尝试,我想出了如何解决这个问题。尽管如此,它有点奇怪,并不能完全满足我。它只对我有用,当我将 digest 设置为 true 并提供到 manifest 的路径时:

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

知道这个“逻辑”的背后是什么会很有趣。

【讨论】:

    猜你喜欢
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    相关资源
    最近更新 更多