没有路由匹配 [GET] /assets
我有一个 Rails 应用程序,我正在尝试在生产环境中测试它。我运行了 RAILS_ENV=生产 rake asset:precompile
,它在 /public/assets 中生成了我的所有资产。问题是,当我启动我的应用程序时,我得到:
ActionController::RoutingError (No route matches [GET] "/assets/application-eff78fd67423795a7be3aa21512f0bd2.css"):
此文件确实存在于 /public/assets/application-eff78fd67423795a7be3aa21512f0bd2.css
。
关于为什么我收到此 RoutingError
有什么想法吗?
I have a Rails app that I'm trying to test in the production environment. I ran RAILS_ENV=production rake assets:precompile
which generated all of my assets in /public/assets. The problem is that when I start my app w/ RAILS_ENV=production rails s thin
I get:
ActionController::RoutingError (No route matches [GET] "/assets/application-eff78fd67423795a7be3aa21512f0bd2.css"):
This file does exist though at /public/assets/application-eff78fd67423795a7be3aa21512f0bd2.css
.
Any thoughts as to why I'm getting this RoutingError
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
在生产模式下,Rails 将不负责提供静态资产。因此,您会收到此错误。 Thin 也不会这样做,因为它只是 Rails 的包装。
这是由应用程序中的
config/environments/Production.rb
中的此设置控制的:或者在 Rails 5 中:
或者将
ENV['RAILS_SERVE_STATIC_FILES']
设置为 true。您可以设置为 true 或使用真正的服务器(如 Apache 或 Nginx)来提供静态资产。我怀疑 Pow 也可能这么做。
如果您使用 Heroku,他们建议使用
rails_12factor
gem,它默认启用此设置。将 gem 放入Gemfile
中的product
组中,如下所示:In production mode, Rails will not be responsible for serving static assets. Therefore, you are getting this error. Thin won't do it either, since it's just a wrapper around Rails.
This is controlled by this setting in
config/environments/production.rb
in your application:Or in Rails 5:
Or set
ENV['RAILS_SERVE_STATIC_FILES']
to true.You can either set to that
true
or use a real server like Apache or Nginx which will serve the static assets. I suspect Pow may also do it.If you're on Heroku, they recommend the use of the
rails_12factor
gem which enables this setting by default. Place the gem into aproduction
group in yourGemfile
, like this:除了 Ryan 上面所说的之外,Rails 资产管道指南还描述了如何设置 Apache 或 nginx 来为您提供静态资产。
http://guides.rubyonrails.org/asset_pipeline.html
您确实应该设置 nginx 或 Apache 来提供静态资产,如下所示与 mongrel/thin/unicorn 相比,它们针对此任务进行了更好的优化。
Adding to what Ryan said above, the Rails asset pipeline guide describes how to setup Apache or nginx to serve the static assets for you.
http://guides.rubyonrails.org/asset_pipeline.html
You really should setup nginx or Apache to serve static assets, as they're much better optimized for this task than mongrel/thin/unicorn.
刚刚解决了同样的问题。就我而言,瑞安的回答没有帮助。 Bratsche 指出了 Rails Guides,不幸的是这对我来说也不起作用。不过,该资源很有帮助。所以我从那里获取了 Nginx 配置并添加了 root 指令,指向公共目录。没有这个就行不通。
重启nginx,就可以了。
Just solved the same problem. In my case Ryan's answer was not helpful. Bratsche pointed to the Rails Guides, unfortunately this didn't work for me too. However the resource was helpful. So I took Nginx configuration from there and added the root directive, pointing to the public directory. Without this it doesn't work.
Restart nginx, and that's it.
在 Rails 5 中,
config.serve_static_files
选项已更改,因此现在您需要在本地提供资源。
In rails 5, the
config.serve_static_files
option has changed, so now you need to haveto serve assets locally.
事实上,您不需要修改任何默认配置。
您只需再次重新编译资产文件。
删除公共/资产
资源编译
Indeed you didn't need to modify any default configs.
You just recompile assets file again.
remove public/assets
assets compile
Rails 4.2 在您的 config/environments/ staging.rb 和 production.rb 文件中添加/更改了这一行:
如果未设置 RAILS_SERVE_STATIC_FILES,并且您是来自 Rails 服务器的服务资产(如 Unicorn),则它将默认为“ false”,就会出现路由错误。
这是一个简单的修复:
Rails 4.2 has added/changed this line in your config/environments/ staging.rb and production.rb files:
If RAILS_SERVE_STATIC_FILES is not set, and you are service assets from your Rails server (like with Unicorn), then it will default to "false", and the RoutingError will occur.
This is an easy fix:
尝试下面的代码:
config/environments/development.rb
然后运行命令:
然后将所有编译文件和清单文件推送到服务器。
try below code:
config/environments/production.rb
then run command:
then push all compiles files and manifest file to server.
我使用 mina+puma+nginx 部署我的 Rails 5 应用程序,我
已经检查了 config/environments/Production.rb
NGINX处理这个问题,正确配置
它就可以正常工作。
I use mina+puma+nginx to deploy my Rails 5 application, I got
check config/environments/production.rb
NGINX already handles this, config it corretcly
things will work fine.
如果有人在测试环境中遇到与我相同的错误,这对我有帮助:
然后:
找到
spring server
进程和他的 PID,然后通过以下方式杀死它:If somebody get here with the same error in the test environment as I do, here's what helped me:
then:
to find
spring server
process and his PID then kill it via: