样式表或JavaScript文件的Rails 404错误

时间:2010-08-05 00:42:30

标签: ruby-on-rails ruby capistrano unicorn

Rails无法加载(404错误)CSS&生产中的JS文件,但在开发中加载它们没有问题。

我正在使用Capistrano进行部署和运行Rails 3.我的开发路径是 / www / myapp 但我的生产路径是 / www / myapp / current

应用程序本身似乎工作正常,因此问题似乎与CSS / JS文件隔离。

我尝试在 environments / production.rb 中将 RAILS_ROOT 变量设置为 / www / myapp / current ,但它没有任何区别,文件仍然无法加载。

这是生产日志中的完整堆栈:

Started GET "/stylesheets/scaffold.css?1280867531" for 98.173.61.21 at 2010-08-04 17:04:05 -0700

ActionController::RoutingError (No route matches "/stylesheets/scaffold.css"):
/usr/local/rvm/gems/ruby-1.9.2-rc2/gems/actionpack-3.0.0.beta4/lib/action_dispatch/middleware/show_exceptions.rb:55:in `call'
  /usr/local/rvm/gems/ruby-1.9.2-rc2/gems/railties-3.0.0.beta4/lib/rails/rack/logger.rb:14:in `call'
  /usr/local/rvm/gems/ruby-1.9.2-rc2/gems/rack-1.1.0/lib/rack/runtime.rb:17:in `call'
  /usr/local/rvm/gems/ruby-1.9.2-rc2/gems/rack-1.1.0/lib/rack/lock.rb:11:in `block in call'
  <internal:prelude>:10:in `synchronize'
  /usr/local/rvm/gems/ruby-1.9.2-rc2/gems/rack-1.1.0/lib/rack/lock.rb:11:in `call'
  /usr/local/rvm/gems/ruby-1.9.2-rc2/gems/railties-3.0.0.beta4/lib/rails/application.rb:145:in `call'
  /usr/local/rvm/gems/ruby-1.9.2-rc2/gems/railties-3.0.0.beta4/lib/rails/application.rb:81:in `method_missing'
  /usr/local/rvm/gems/ruby-1.9.2-rc2/gems/unicorn-1.1.2/lib/unicorn.rb:642:in `process_client'
  /usr/local/rvm/gems/ruby-1.9.2-rc2/gems/unicorn-1.1.2/lib/unicorn.rb:715:in `block in worker_loop'
  /usr/local/rvm/gems/ruby-1.9.2-rc2/gems/unicorn-1.1.2/lib/unicorn.rb:713:in `each'
  /usr/local/rvm/gems/ruby-1.9.2-rc2/gems/unicorn-1.1.2/lib/unicorn.rb:713:in `worker_loop'
  /usr/local/rvm/gems/ruby-1.9.2-rc2/gems/unicorn-1.1.2/lib/unicorn.rb:604:in `block (2 levels) in spawn_missing_workers'
  /usr/local/rvm/gems/ruby-1.9.2-rc2/gems/unicorn-1.1.2/lib/unicorn.rb:601:in `fork'
  /usr/local/rvm/gems/ruby-1.9.2-rc2/gems/unicorn-1.1.2/lib/unicorn.rb:601:in `block in spawn_missing_workers'
  /usr/local/rvm/gems/ruby-1.9.2-rc2/gems/unicorn-1.1.2/lib/unicorn.rb:597:in `each'
  /usr/local/rvm/gems/ruby-1.9.2-rc2/gems/unicorn-1.1.2/lib/unicorn.rb:597:in `spawn_missing_workers'
  /usr/local/rvm/gems/ruby-1.9.2-rc2/gems/unicorn-1.1.2/lib/unicorn.rb:611:in `maintain_worker_count'
  /usr/local/rvm/gems/ruby-1.9.2-rc2/gems/unicorn-1.1.2/lib/unicorn.rb:270:in `start'
  /usr/local/rvm/gems/ruby-1.9.2-rc2/gems/unicorn-1.1.2/lib/unicorn.rb:29:in `run'
  /usr/local/rvm/gems/ruby-1.9.2-rc2/gems/unicorn-1.1.2/bin/unicorn:123:in `<top (required)>'
  /usr/local/rvm/gems/ruby-1.9.2-rc2/bin/unicorn:19:in `load'
  /usr/local/rvm/gems/ruby-1.9.2-rc2/bin/unicorn:19:in `<main>'

2 个答案:

答案 0 :(得分:2)

错误消息看起来像您的Rails应用程序正在获取静态文件的请求。默认情况下,Rails 3不提供静态文件,因为Web服务器可以做得更好。您应该检查您的网络服务器的配置。它应配置为首先查看,如果请求的公共目录中存在静态文件,并且仅将请求转发给Rails应用程序,如果没有静态文件。

或者,您可以启用Rails以在config / environments / production.rb中提供config.serve_static_assets = true的静态文件。但是,这不建议在生产中使用,因为您真的不应该浪费Rails应用程序的处理资源只是为了提供静态文件。最好告诉网络服务器这样做。

答案 1 :(得分:0)

我通过运行此命令解决了这个问题:

RAILS_ENV=production rake assets:precompile

参考:http://guides.rubyonrails.org/asset_pipeline.html

我的环境为Nginx + Unicorn + Rails4