生产环境下Rails资源路径缺少指纹

7
我们刚刚将一个Rails 4.0.3应用部署到生产环境中,并发现由stylesheet_link_tagjavascript_link_tag生成的资源路径缺少指纹。因此,页面请求的不是类似于application-c841bd1c82c25bb1de8452d2338479f7.js这样的内容,而只是请求application.jsRAILS_ENV=production bundle exec rake assets:precompile执行成功并生成了带有指纹的文件。
看起来与此相关的是config/environments/production.rb中的一些部分:
# 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

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

这是运行在我们自己的Apache服务器上,而不是Heroku。我已经查找了很多类似问题,但那些疑难解答步骤都没有帮助到这里。谢谢。
更多信息:
如果有帮助的话,这里是我们配置文件的完整内容(已移除注释行):
application.rb
require File.expand_path('../boot', __FILE__)

require 'rails/all'

if defined?(Bundler)
  # If you precompile assets before deploying to production, use this line
  Bundler.require(*Rails.groups(:assets => %w(development test)))
  # If you want your assets lazily compiled in production, use this line
  # Bundler.require(:default, :assets, Rails.env)
end

module Ctrc
  class Application < Rails::Application
    config.ceal.application_title = "CTRC Budgeting"

    config.encoding = "utf-8"

    config.filter_parameters += [:password]

    config.active_support.escape_html_entities_in_json = true

    config.assets.enabled = true
    config.assets.version = '1.0'

    config.pmacs_redmine.project_identifier = 'itmat-gcrc'

    config.app_data_path = '/data/web/apps/itmat/ctrc'
    config.paperclip_defaults = {
      path: "/data/web/apps/itmat/ctrc/:attachment/:id/:style/:basename.:extension"
    }

    if ENV['RAILS_RELATIVE_URL_ROOT']
      config.assets.prefix = ENV['RAILS_RELATIVE_URL_ROOT'] + '/assets'
    end
  end
end

production.rb

Ctrc::Application.configure do
  config.cache_classes = true

  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true

  config.serve_static_assets = false
  config.assets.compress = true
  config.assets.compile = false
  config.assets.digest = true

  config.i18n.fallbacks = true

  config.active_support.deprecation = :notify

  config.eager_load = true

end

我知道应用程序正在生产模式下运行,因为如果我设置
config.assets.compile = true

在该文件中,CSS和JavaScript已经被编译并正确请求。
我将这些资源包含在页面中,如下所示:
<%= stylesheet_link_tag    "application", media: "all" %>
<%= javascript_include_tag "application" %>

但它仍然以这样的方式生成到那些资源的链接:
<link href="/apps/itmat/ctrc/stylesheets/application.css" media="all" rel="stylesheet">
<script src="/apps/itmat/ctrc/javascripts/application.js"></script>

我期望看到的是带有指纹的链接,而不是现在的样子。


config.serve_static_assets = false 是吗? - DVG
你在 config/application.rb 文件中有这个 config.assets.enabled = true 吗? - usha
@RubyRacer 这不是问题。而且建议使用 false - Eyeslandic
我发现可以通过两次编译资产来解决这个问题。首先使用指定的RAILS_RELATIVE_URL_ROOT进行编译,然后再次进行编译而不使用它。如果我跳过其中任何一个或颠倒顺序,我最终会得到一些或所有资产的404错误。 - barendt
我曾经遇到过类似的问题。当时我(很傻地)部署了一个不同于我所检出的分支。因此,我在我的特性分支上对环境配置文件所做的更改并没有生效——因为它们并不在服务器上!显然,解决方法就是部署我的特性分支。 - user3680688
显示剩余6条评论
2个回答

1
我遇到了类似的问题,最终发现这是由于AssetSync宝石出了问题。要么将config.assets.compile = true进行设置,要么移除gem可以解决这个问题。如果你使用CDN,那么实时编译资产并让Rails提供资产可能是可接受的,但通常推荐采用其他方法。

0

对于Rails 4.x,请确保设置以下内容:

config/application.rb

config.assets.enabled = true
config.assets.version = '1.0'

config/environments/production.rb

config.assets.compile = false

我已经仔细检查了所有这些设置,它们都是正确的。 - barendt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接