我们刚刚将Rails 4.0.3应用程序部署到生产环节,并发现stylesheet_link_tag
和javascript_link_tag
生成的资产路径缺少指纹。因此,页面只是请求application-c841bd1c82c25bb1de8452d2338479f7.js
而不是请求application.js
之类的内容。
RAILS_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>
而不是我希望看到的指纹链接。
答案 0 :(得分:1)
我有一个类似的问题,结果证明是gem AssetSync的一个问题。设置config.assets.compile = true
或删除gem都解决了问题。如果您正在使用CDN,那么动态编译资产并让Rails为您的资产提供服务可能是可以接受的,但通常建议采用其他方法。
答案 1 :(得分:0)
对于rails 4.x,请务必进行以下设置:
配置/ application.rb中
config.assets.enabled = true
config.assets.version = '1.0'
配置/环境/ production.rb
config.assets.compile = false