【问题标题】:Rails 3.1 assets has no fingerprint in productionRails 3.1 资产在生产中没有指纹
【发布时间】:2025-11-26 12:05:01
【问题描述】:

刚开始适应rails 3.1,我开始写coffeescript和sass,在开发中一切正常。当我在生产中运行服务器时,我只得到:

  <link href="/stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" />
  <script src="/javascripts/application.js" type="text/javascript"></script>

在页面的源代码中,没有生成哈希码,两个资产都有路由错误:

Routing Error
No route matches [GET] "/stylesheets/application.css"

这是什么原因?我是不是忘了做点什么?

环境/production.rb 中的设置:

# Settings specified here will take precedence over those in config/application.rb

  # Code is not reloaded between requests
  config.cache_classes = true

  # Full error reports are disabled and caching is turned on
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true

  # Disable Rails's static asset server (Apache or nginx will already do this)
  config.serve_static_assets = false

  # Compress JavaScripts and CSS
  config.assets.compress = true



    # Don't fallback to assets pipeline if a precompiled asset is missed
  config.assets.compile = false

  # Generate digests for assets URLs
  config.assets.digest = true

  config.active_support.deprecation = :notify

非常感谢。

添加更多信息:

在 layouts/application.html.erb 中,我使用以下内容来包含资产:

  <%= stylesheet_link_tag "application" %>
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>

我试过 bundle exec rake assets:precompile 运行没有输出任何东西,然后运行 ​​rails s -e production ,问题仍然存在。

我也尝试设置config.assets.compile = true,然后运行rails s -e production,问题依旧。

请帮忙。

更多信息。 我已经看到编译后的js和css是在public/assets文件夹中生成的,但是在生产环境中,文件包含在没有哈希码的情况下。

帮助。

解决方案: 刚刚再次检查了我的项目,发现根本原因是我在编辑application.rb以支持mongodb时。不小心评论了

require "sprockets/railtie"

取消注释然后一切都很好。

留给别人提醒我的菜鸟错误。

非常感谢理查德。您的答案不是最终答案,但它有很大帮助,真的值得一票。

【问题讨论】:

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


    【解决方案1】:

    检查您是否在 application.rb 中打开了管道:

    config.assets.enabled = true

    您是否使用正确的辅助方法来编写标签?辅助方法的路径中不应包含 /styleheets 和 /javascript。像这样(在erb里面):

    javascript_include_tag "应用程序" stylesheet_link_tag "应用程序"

    您还需要在部署过程中运行预编译任务来创建文件,因为您已将 compile 设置为 false。

    asset pipeline guide 展示了如何使用 capistrano 进行设置。

    【讨论】:

    • 谢谢理查德,我在主帖中添加了附加信息。我必须使用 capistrano 才能获得此功能吗?我只是想在本地运行“rails s -e production”。
    • 如果你是本地的,那么你必须在本地运行它。如果这是您部署到生产生产环境的方式,您只需要 Capistrano 任务。将您的应用升级到 3.1.1.rc2 并重新运行该任务。我认为预编译任务中有一些修复程序可能会解决此问题。
    最近更新 更多