生产环境下Rails 3.1的资源没有指纹

9

我刚开始适应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"

这是什么原因?我是否忘记做了什么?

在environments/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"

取消注释后一切正常。

留下此处,提醒其他人不要犯我的新手错误。

非常感谢Richard。虽然你的答案并不是最终答案,但对我帮助很大,真心值得点赞。

1个回答

3

请检查应用程序文件application.rb中pipeline是否已经开启:

config.assets.enabled = true

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

javascript_include_tag "application"
stylesheet_link_tag "application"

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

资产管道指南展示了如何使用capistrano进行设置。


谢谢Richard,我已经在主贴中添加了额外的信息。我是否必须使用Capistrano才能获得此功能?我只是想在本地运行“rails s -e production”。 - larryzhao
如果你是本地的,那么你必须在本地运行它。只有当你部署到生产环境时才需要使用Capistrano任务。将你的应用程序升级到3.1.1.rc2版本并重新运行该任务。我认为在预编译任务中有一些修复措施可以解决这个问题。 - Richard Hulse

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