Rails生产静态文件路由错误

发布于 2024-10-16 11:47:56 字数 973 浏览 2 评论 0原文

当我在测试/开发中本地运行我的应用程序时,我的观点很好地出现,一切都很愉快。当我尝试导航到在远程服务器/本地生产服务器上运行的相同 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羁绊已千年 2024-10-23 11:47:57

您使用的是 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 to true in your production.rb file.

轻许诺言 2024-10-23 11:47:57

在 Rails5 中,您应该

"config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?" 

在 config/enviroment/development.rb 中进行注释

In Rails5, you should comment

"config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?" 

in config/enviroment/production.rb

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文