问题描述
我有一个Rails应用,正在尝试在生产环境中进行测试.我运行了RAILS_ENV=production rake assets:precompile
,它在/public/assets中生成了我的所有资产.问题是,当我使用RAILS_ENV=production rails s thin
启动我的应用时,我得到了:
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"):
此文件确实存在,但位于/public/assets/application-eff78fd67423795a7be3aa21512f0bd2.css
.
This file does exist though at /public/assets/application-eff78fd67423795a7be3aa21512f0bd2.css
.
关于我为什么要得到这个RoutingError
的任何想法?
Any thoughts as to why I'm getting this RoutingError
?
推荐答案
在生产模式下,Rails将不负责提供静态资产.因此,您会收到此错误. Thin不会做任何事情,因为它只是Rails的包装.
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.
这由您应用程序中config/environments/production.rb
中的此设置控制:
This is controlled by this setting in config/environments/production.rb
in your application:
config.serve_static_files = false
或在Rails 5中:
Or in Rails 5:
# config/environments/production.rb
config.public_file_server.enabled = true
或将ENV['RAILS_SERVE_STATIC_FILES']
设置为true.
您可以设置为true
,也可以使用将为静态资产提供服务的真实服务器(如Apache或Nginx).我怀疑Pow也可能会这样做.
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.
如果您使用的是Heroku,他们建议使用rails_12factor
gem,默认情况下启用此设置.将宝石放入Gemfile
中的production
组中,如下所示:
If you're on Heroku, they recommend the use of the rails_12factor
gem which enables this setting by default. Place the gem into a production
group in your Gemfile
, like this:
group :production do
gem 'rails_12factor'
end
这篇关于没有路线符合[GET]/资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!