Rails资产管道在生产环境中不起作用?

时间:2011-10-03 22:51:55

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

我最近将应用程序从Rails 3.0升级到3.1。我已经按照我可以找到的任何说明来启用资产管道,但在生产环境中它总是失败:

<%= javascript_include_tag "application" %>

给了我

<script src="/javascripts/application.js" type="text/javascript"></script>

缺少摘要,我收到以下错误:

cache: [GET /javascripts/application.js] miss
Started GET "/javascripts/application.js" for 127.0.0.1 at 2011-10-03 23:31:36 +0100
ActionController::RoutingError (No route matches [GET] "/javascripts/application.js"):

我在application.rb中试过了这些设置的变体:

require File.expand_path('../boot', __FILE__)

#require 'rails/all'
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"

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 Blog
  class Application < Rails::Application
    config.autoload_paths += %W(#{config.root}/lib)
    config.encoding = "utf-8"
    config.filter_parameters += [:password]
    config.assets.enabled = true
    config.assets.version = '1.0'
  end
end

和完整的production.rb(减去一些评论)

Blog::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
end

我已经运行了rake assets:precompile任务。

我错过了任何明显的步骤吗?

修改:其他一些细节:

我的资源位于app/assets文件夹中。 app/assets/imagesapp/assets/javascriptsapp/assets/stylesheets

我在public/assets目录中看到我的文件包含名称和摘要。

app/assets/javascripts/application.js确实编译为类似public/assets/application-6ec417a53cb2bdb949966a153a61e7b1.js的内容。它们最终位于public目录中。

2 个答案:

答案 0 :(得分:12)

Sprockets没有加载。

为了删除以前版本的rails中的活动记录(这个问题Remove ActiveRecord in Rails 3 (beta)),require 'rails/all'

取代
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"

这里缺少的是sprockets/railtie

答案 1 :(得分:3)

请参阅Upgrading to Rails 3.1 Railscast

确保您的资源位于app/assets文件夹中。 app/assets/imagesapp/assets/javascriptsapp/assets/stylesheets

执行rake assets:precompile

如果启用,您应该会看到app/public/assets目录中生成的文件包含名称和摘要。

app/assets/javascripts/application.js将汇编为/assets/application-6ec417a53cb2bdb949966a153a61e7b1.js

如果资产的名称与上述内容相似,则Production.rb应具有以下配置:

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

如果您查看网页来源,您应该会看到类似于以下内容的内容:

<script src="/assets/application-6ec417a53cb2bdb949966a153a61e7b1.js" type="text/javascript"></script>

尝试转到http://example.com//assets/application-6ec417a53cb2bdb949966a153a61e7b1.js

手动加载文件

文件应加载,如果没有尝试检查权限和更多日志。