【问题标题】:Rails not serving assets in production or staging environmentsRails 不为生产或登台环境中的资产提供服务
【发布时间】:2013-03-04 16:57:19
【问题描述】:

在调试this problem 的过程中,我尝试在本地以生产模式运行我的应用程序,但它没有提供任何资产。此外,我在 Heroku 应用程序(独立于我的生产 Heroku 应用程序)中有一个 staging 环境,它现在也显示没有任何资产的 HTML。

为了调试,我:

  1. 杀死服务器
  2. 清除 tmp/cache/assets
  3. 删除公共/资产
  4. 运行rake assets:precompile
  5. 启动服务器rails s -e production
  6. 访问页面并打开网络检查器,当单击 application.css 链接的展开箭头时,它显示 Reload the page to get source for: http://localhost:3000/assets/application-e1f3e0c864a153c7iu66f8772a886376.css
  7. 重新加载页面没有任何作用。

Production.rb

config.cache_classes = true
config.consider_all_requests_local       = false
config.action_controller.perform_caching = true
config.serve_static_assets = true
config.static_cache_control = "public, max-age=3600"
config.assets.compress = false
config.assets.compile = false
config.assets.digest = true

Staging.rb

config.cache_classes = true
config.consider_all_requests_local       = false
config.action_controller.perform_caching = true
config.serve_static_assets = true
config.static_cache_control = "public, max-age=3600"
config.assets.compress = false
config.assets.compile = false
config.assets.digest = true

Application.rb

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

下面是我如何在 layout/application.html.erb 中链接样式表和 javascript:

<%= stylesheet_link_tag "application", :media => "screen, handheld" %>
<%= javascript_include_tag "application" %>

【问题讨论】:

  • 您能展示一下您是如何访问这些资产的吗?喜欢= stylesheet_link_tag "application", :media =&gt; "all"
  • 我已经更新了问题。
  • 您使用的是什么 Rack 中间件?切换到瘦需要我设置config.assets.compile = true 而不是默认的false - 但如果我使用Passenger 则不需要。

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


【解决方案1】:

所以问题是内存存储设置为 config.cache_store = :dalli_store 导致错误并将其设置为 config.cache_store = :memory_store 解决了它。

【讨论】:

  • 出于某种原因,这似乎根本不相关。这怎么可能?
  • @CourtS:在开发/生产中缓存资产时,Sprockets 将use the default cache store。默认为文件存储。
【解决方案2】:

这是一种猜测,但编译资产不需要设置为 true 吗?

config.assets.compile = true

我认为您需要像这样编译资产:

rake assets:precompile RAILS_ENV='production'

【讨论】:

  • 不正确,config.assets.compile = true 管道中的所有资产请求都由 Sprockets 直接处理。此外,指定环境并没有什么不同。
  • 您是否尝试按照我的建议预编译资产?或者根据这个问题stackoverflow.com/questions/7340635/…,你可以尝试设置config.serve_static_assets = false
  • 是的。我还更改了config.serve_static_assets = false,并且网络检查器中资产的摘要 url 与 public/assets 中的文件匹配,但它仍然是 get http:localhost:3000/assets/application-e1f3e0c864a153c75866f8772a056376.css 404 (not found)。我还重新启动了服务器并硬刷新了浏览器。
  • 我相信这些资产会被编译到您项目的 /public 目录中。这些文件是否真的存在于该文件夹中?
  • 应该有config.serve_static_assets = true,除非您有单独的服务器为这些资产提供服务。
猜你喜欢
  • 2013-07-28
  • 1970-01-01
  • 2014-04-13
  • 2023-03-13
  • 2014-03-11
  • 2018-09-20
  • 2020-06-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多