Kyl*_*cot 136 ruby-on-rails ruby-on-rails-3 asset-pipeline
我有一个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"):
Run Code Online (Sandbox Code Playgroud)
这个文件确实存在于/public/assets/application-eff78fd67423795a7be3aa21512f0bd2.css.
有什么想法为什么我得到这个RoutingError?
Rya*_*igg 224
在生产模式下,Rails不负责提供静态资产.因此,您收到此错误.Thin也不会这样做,因为它只是Rails的包装器.
这由config/environments/production.rb您的应用程序中的此设置控制:
config.serve_static_files = false
Run Code Online (Sandbox Code Playgroud)
或者在Rails 5中:
# config/environments/production.rb
config.public_file_server.enabled = true
Run Code Online (Sandbox Code Playgroud)
或者设置ENV['RAILS_SERVE_STATIC_FILES']为true.
您可以设置为true或使用Apache或Nginx等真实服务器来提供静态资产.我怀疑Pow也可能这样做.
如果您使用Heroku,他们建议使用rails_12factorgem,默认情况下启用此设置.将宝石放入production您的组中Gemfile,如下所示:
group :production do
gem 'rails_12factor'
end
Run Code Online (Sandbox Code Playgroud)
bra*_*che 12
除了上面提到的Ryan之外,Rails资产管道指南还介绍了如何设置Apache或nginx来为您提供静态资产.
http://guides.rubyonrails.org/asset_pipeline.html
你真的应该设置nginx或Apache来提供静态资产,因为它们比mongrel/thin/unicorn更适合这项任务.
刚刚解决了同样的问题.在我的情况下,瑞恩的回答没有帮助.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;
}
Run Code Online (Sandbox Code Playgroud)
重启nginx,就是这样.