0

当我从 3.1.1 Rails 升级到 3.2.8 时,看到样式表、图像的路由错误真的很令人沮丧。尝试了“bundle exec rails server”命令,还尝试了一个一个地在文件中添加宝石,也没有用:(

ActionController::RoutingError (No route matches [GET] "/stylesheets/application.css"):
  actionpack (3.2.8) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (3.2.8) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.8) lib/rails/rack/logger.rb:26:in `call_app'
  railties (3.2.8) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.8) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.1) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.8) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.1) lib/rack/lock.rb:15:in `call'
  actionpack (3.2.8) lib/action_dispatch/middleware/static.rb:62:in `call'
  railties (3.2.8) lib/rails/engine.rb:479:in `call'
  railties (3.2.8) lib/rails/application.rb:223:in `call'
  rack (1.4.1) lib/rack/content_length.rb:14:in `call'
  railties (3.2.8) lib/rails/rack/log_tailer.rb:17:in `call'
  rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
4

1 回答 1

1

链接到 rails 中的资产时,请始终使用asset_pathasset_url或依次调用它们的辅助函数之一(image_pathimage_urljavascript_include_tagstylesheet_link_tag)。

在您的情况下,看起来您/stylesheets/application.css在应用程序布局中进行了硬编码。简单地将该代码更改为类似

<%= stylesheet_link_tag :application %>

它应该正确地指向/assets/application.css开发和/assets/application-<sha1 hash>.css生产中(编译资产时)。

此外,如果您有在本地编译的资产(如果您有任何情况就是这种情况。如果您在本地开发,public/assets您可能希望这样做),那么您需要以下行:rm -rf public/assetsconfig/application.rb

config.serve_static_assets = true
于 2012-08-13T06:55:44.527 回答