Rails生产静态文件路由错误
当我在测试/开发中本地运行我的应用程序时,我的观点很好地出现,一切都很愉快。当我尝试导航到在远程服务器/本地生产服务器上运行的相同 erb 文件时,我收到如下错误:
ActionController::RoutingError (No route matches "/stylesheets/scaffold.css")
我在这里看到了类似的问题,但没有一个能够解决我的问题。我发现的最接近答案的是这里的第一个答案: 样式表或 JavaScript 文件出现 Rails 404 错误
据我了解,最好的办法是配置我的网络服务器来提供静态文件。我如何在本地/Heroku 上执行此操作?
更新
根据 raid Five 的建议,我将 config.serve_static_assets 从 false 更改为 true,这解决了我的问题。但是,我看到在 production.rb 中说 Apache 或 nginx 应该已经在提供静态资产服务。以这种方式提供静态资产是否不太好/不专业?如果是的话,如果我使用 Heroku,我将如何实现预期的结果?
更新 2
显然 Heroku 自动执行此操作,我有一个额外的逗号导致了恶作剧。我可以使用 以下提示查看扩展的 Heroku 日志 来追踪问题。谢谢!
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")
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
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?
Update
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?
Update 2
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用的是 Rails 3 吗?默认情况下,Rails 3 / webrick 不在生产模式下提供静态文件。 设置为
true
轻松启用此功能。您可以通过在
development.rb
文件中将config.serve_static_assets
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_assets
totrue
in yourproduction.rb
file.在 Rails5 中,您应该
在 config/enviroment/development.rb 中进行注释
In Rails5, you should comment
in
config/enviroment/production.rb