Ruby-on-rails 没有路由匹配 [GET] /assets
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7829480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No route matches [GET] /assets
提问by Kyle Decot
I have a Rails app that I'm trying to test in the production environment. I ran RAILS_ENV=production rake assets:precompilewhich generated all of my assets in /public/assets. The problem is that when I start my app w/ RAILS_ENV=production rails s thinI get:
我有一个 Rails 应用程序,我想在生产环境中测试它。我跑了RAILS_ENV=production rake assets:precompile它在 /public/assets 中生成了我的所有资产。问题是,当我启动我的应用程序时,RAILS_ENV=production rails s thin我得到:
ActionController::RoutingError (No route matches [GET] "/assets/application-eff78fd67423795a7be3aa21512f0bd2.css"):
This file does exist though at /public/assets/application-eff78fd67423795a7be3aa21512f0bd2.css.
该文件确实存在于/public/assets/application-eff78fd67423795a7be3aa21512f0bd2.css.
Any thoughts as to why I'm getting this RoutingError?
关于为什么我得到这个的任何想法RoutingError?
回答by Ryan Bigg
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.
在生产模式下,Rails 将不负责提供静态资产。因此,您收到此错误。Thin 也不会这样做,因为它只是 Rails 的包装器。
This is controlled by this setting in config/environments/production.rbin your application:
这由config/environments/production.rb您的应用程序中的此设置控制:
config.serve_static_files = false
Or in Rails 5:
或者在 Rails 5 中:
# config/environments/production.rb
config.public_file_server.enabled = true
Or set ENV['RAILS_SERVE_STATIC_FILES']to true.
或者设置ENV['RAILS_SERVE_STATIC_FILES']为true。
You can either set to that trueor use a real server like Apache or Nginx which will serve the static assets. I suspect Pow may also do it.
您可以设置为该值,也可以true使用像 Apache 或 Nginx 这样的真实服务器来为静态资产提供服务。我怀疑 Pow 也可能会这样做。
If you're on Heroku, they recommend the use of the rails_12factorgem which enables this setting by default. Place the gem into a productiongroup in your Gemfile, like this:
如果您使用 Heroku,他们建议使用rails_12factor默认启用此设置的gem。将 gem 放入production您的组中Gemfile,如下所示:
group :production do
gem 'rails_12factor'
end
回答by bratsche
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.
除了 Ryan 上面所说的之外,Rails 资产管道指南描述了如何设置 Apache 或 nginx 为您提供静态资产。
http://guides.rubyonrails.org/asset_pipeline.html
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.
你真的应该设置 nginx 或 Apache 来提供静态资产,因为它们比 mongrel/thin/unicorn 更适合这个任务。
回答by valk
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 rootdirective, pointing to the public directory. Without this it doesn't work.
刚刚解决了同样的问题。就我而言,瑞安的回答没有帮助。Bratsche 指着 Rails 指南,不幸的是这对我也不起作用。但是,该资源很有帮助。所以我从那里获取了 Nginx 配置并添加了root指令,指向公共目录。没有这个就行不通。
# serve static assets
location ~ ^/assets/ {
expires 1y;
root /path/to/my/cool_project/public;
add_header Cache-Control public;
add_header ETag "";
break;
}
Restart nginx, and that's it.
重启 nginx,就是这样。
回答by Albert.Qing
Indeed you didn't need to modify any default configs. You just recompile assets file again.
实际上,您不需要修改任何默认配置。您只需再次重新编译资产文件。
remove public/assets
删除公共/资产
1.rake assets:clobber RAILS_ENV=production
1.rake assets:clobber RAILS_ENV=production
assets compile
资产编译
2.rake assets:precompile RAILS_ENV=production
3.restart server,eg(nginx)
2.rake资产:预编译RAILS_ENV=production
3.重启服务器,例如(nginx)
回答by Obromios
In rails 5, the config.serve_static_filesoption has changed, so now you need to have
在 rails 5 中,config.serve_static_files选项已更改,因此现在您需要拥有
config.public_file_server.enabled = true
to serve assets locally.
本地服务资产。
回答by puneet18
try below code:
试试下面的代码:
config/environments/production.rb
配置/环境/production.rb
config.assets.compile = true
then run command:
然后运行命令:
RAILS_ENV=production rake assets:precompile
then push all compiles files and manifest file to server.
然后将所有编译文件和清单文件推送到服务器。
回答by Martin Sommer
Rails 4.2 has added/changed this line in your config/environments/ staging.rb and production.rb files:
Rails 4.2 在您的 config/environments/staging.rb 和 production.rb 文件中添加/更改了这一行:
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
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.
如果未设置 RAILS_SERVE_STATIC_FILES,并且您是 Rails 服务器的服务资产(如 Unicorn),则默认为“false”,并且会发生 RoutingError。
This is an easy fix:
这是一个简单的修复:
config.serve_static_files = true
回答by ToTenMilan
If somebody get here with the same error in the test environment as I do, here's what helped me:
如果有人在测试环境中遇到与我相同的错误,那么这对我有帮助:
rails assets:clobber assets:precompile RAILS_ENV=test
then:
然后:
ps axu | grep your-username
to find spring serverprocess and his PID then kill it via:
找到spring server进程和他的PID然后通过以下方式杀死它:
kill <spring-server-PID>
回答by Feuda
I use mina+puma+nginxto deploy my Rails 5 application, I got
我使用mina+ puma+ nginx来部署我的 Rails 5 应用程序,我得到了
ActionController::RoutingError (No route matches [GET] "/assets/application-658cf2ab3ac93aa5cb41a762b52cf49d7184509c307922cd3fbb61b237a59c1a.css")
check config/environments/production.rb
检查配置/环境/production.rb
# Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
NGINX already handles this, config it corretcly
NGINX 已经处理了这个,正确配置它
upstream puma {
server unix:///home/deploy/apps/appname/shared/tmp/sockets/appname-puma.sock;
}
server {
listen 80 default_server deferred;
# server_name example.com;
root /home/deploy/apps/appname/current/public;
access_log /home/deploy/apps/appname/current/log/nginx.access.log;
error_log /home/deploy/apps/appname/current/log/nginx.error.log info;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri @puma;
location @puma {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://puma;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 10M;
keepalive_timeout 10;
}
things will work fine.
一切都会好起来的。

