生产或演示环境下Rails无法提供资产服务

4
在调试这个问题的过程中,我尝试在本地以生产模式运行我的应用程序,但它没有提供任何资产。此外,我有一个Heroku应用程序中的staging环境(与我的生产Heroku应用程序分开),也显示没有任何资产的HTML。
为了调试,我:
  1. 关闭服务器
  2. 清除tmp/cache/assets
  3. 删除public/assets
  4. 运行rake assets:precompile
  5. 启动服务器rails s -e production
  6. 访问页面并打开Web检查器,当单击应用程序.css链接的展开箭头时,它会显示重新加载页面以获取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" %>

1
你能展示一下如何访问资产吗?例如 = stylesheet_link_tag "application", :media => "all" - BBQ Chef
你正在使用哪个Rack中间件?切换到thin需要我将config.assets.compile设置为true,而不是默认的false - 但如果我使用Passenger则不需要。 - sameers
2个回答

7

问题在于内存存储设置为config.cache_store = :dalli_store导致出错,将其设置为config.cache_store = :memory_store解决了问题。


3
由于某种原因,它似乎与任何相关性都没有。这怎么可能? - courtsimas
1
@CourtS:在开发/生产环境中缓存资产时,Sprockets将使用默认的缓存存储。它默认为文件存储。 - James

6
这只是一种猜测,但是否需要编译资产设置为true呢?
config.assets.compile = true

我认为您需要像这样编译资产:
rake assets:precompile RAILS_ENV='production'

5
使用config.assets.compile = true时,所有针对管道中资产的请求都将由Sprockets直接处理。此外,指定环境并不会有任何影响。 - railsuser400
你是否尝试过像我建议的那样预编译资产?或者根据这个问题https://dev59.com/E2s05IYBdhLWcg3wR_22,您可以尝试设置 config.serve_static_assets=false - Catfish
1
是的。我还更改了config.serve_static_assets = false,并且在Web检查器中资产的摘要URL与public / assets中的文件匹配,但它仍然显示“get http:// localhost:3000 / assets / application-e1f3e0c864a153c75866f8772a056376.css 404(未找到)”。我还重新启动了服务器并强制刷新了浏览器。 - railsuser400
我相信资产会被编译到项目的 /public 目录中。这些文件实际上存在于该文件夹中吗? - Catfish
2
除非您有一个单独的服务器提供这些资产,否则应该使用 config.serve_static_assets = true - Clucking Turtle

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