生产静态文件路由错误

生产静态文件路由错误

本文介绍了Rails 生产静态文件路由错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 test/dev 中本地运行我的应用程序时,我的视图很好地出现并且一切都很愉快.当我尝试导航到在我的远程服务器/本地生产服务器上运行的那些相同的 erb 文件时,我收到如下错误:

When I run my app locally in test/dev my views come up nicely and everything is happy. When I try to navigate to those same erb files running on my remote server/local production server I get errors like the following:

ActionController::RoutingError (No route matches "/stylesheets/scaffold.css")

我在 SO 上看到过类似的问题,但没有人能够解决我的问题.我找到的最接近答案的是这里的第一个答案:样式表或 JavaScript 文件的 Rails 404 错误

I've seen similar questions here on SO but none have been able to solve my problem. The closest thing I've found to an answer is the first answer here: Rails 404 error for Stylesheet or JavaScript files

据我所知,最好的办法是配置我的网络服务器以提供静态文件.我如何在本地/在 He​​roku 上执行此操作?

As I understand it the best thing to do would be to configure my webserver to serve static files. How do I do this locally/on Heroku?

更新

根据 raidfive 的建议,我将 config.serve_static_assets 从 false 更改为 true,这解决了我的问题.但是,我看到它在 production.rb 中说 Apache 或 nginx 应该已经在为静态资产提供服务.以这种方式提供静态资产是否不那么好/专业?如果是这样,如果我使用 Heroku,我将如何达到预期的结果?

As per raidfive's suggestion I changed config.serve_static_assets from false to true and this fixed my issue. However, I see that it says in production.rb that Apache or nginx should already be serving static assets. Is it any less good/professional to serve static assets in this way and if so how would I achieve the desired results if I'm using Heroku?

更新 2

显然 Heroku 自动执行此操作,我有一个额外的逗号是造成恶作剧.我能够使用 以下提示查看扩展的 Heroku 日志 追查问题.谢谢!

Apparently Heroku does this automatically, I had an extra comma that was causing the mischief. I was able to look in the extended Heroku logs using the following tip to track down the trouble. Thanks SO!

推荐答案

您在使用 Rails 3 吗?默认情况下,Rails 3/webrick 在生产模式下不提供静态文件.您可以通过设置

Are you using Rails 3? By default Rails 3 / webrick does not serve static files in production mode. You can easily enable this by setting

config.serve_static_assetstrue 在您的 production.rb 文件中.

config.serve_static_assets to true in your production.rb file.

这篇关于Rails 生产静态文件路由错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 13:23