样式表或JavaScript文件的Rails 404错误 [英] Rails 404 error for Stylesheet or JavaScript files

查看:84
本文介绍了样式表或JavaScript文件的Rails 404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Rails can't load (404 error) CSS & JS files on production but has no problem loading them in development.

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

I'm using Capistrano for deployment and running Rails 3. My path on development is /www/myapp but my path on production is /www/myapp/current.

应用程序本身似乎运行良好,所以该问题似乎只存在于CSS / JS文件中。

The application itself seems to work fine, so the issue seems to be isolated to CSS/JS files.

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

I tried setting the RAILS_ROOT variable to /www/myapp/current in environments/production.rb but it did not make any difference, the files still don't load.

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

Here's the full stack from the production log:

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>'


推荐答案

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

The error message looks like your Rails app is getting the request for a static file. Rails 3 does not serve static files by default, since the webserver can do so much better. You should check your webserver's configuration. It should be configured to first look, if a static file exists in the public directory for a request and only forward the request to the Rails app, if there's no static file.

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

Alternatively, you can enable Rails to also serve static files with config.serve_static_assets = true in config/environments/production.rb. However, this is not recommended in production, since you really shouldn't waste processing resources of a Rails app just for serving static files. Better tell the webserver to do so.

这篇关于样式表或JavaScript文件的Rails 404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆