Rails 3.1,资产管道:没有路线匹配
这个问题类似于 为什么对于对资产管道的请求,我是否会收到“无路由匹配”?。
我有一个 Rails 3.0 应用程序,已升级到 3.1 并转换为使用新的资源管道(感谢 RailsCasts #282 和 #279)。
在生产模式下,我看到 application-
但是,如果我将 ?debug_assets=1
添加到 URL 以便我可以查看各个文件,其中一些文件会产生 ActionController::RoutingError (没有路由匹配 [GET] "/assets/
,某些 CSS 文件也是如此。但不是全部,只是一些,我不知道是什么让某些文件执行此操作而其他文件则不执行此操作。
我已经清除了 tmp/cache/* 并重新启动了 Passenger。我已经修改了 config.assets.version
。我已经重新启动了memcached。这些似乎都不能解决这个问题。但奇怪的是,只有当我在 URL 中使用 ?debug_assets=1
时才会出现这种情况;如果没有它,我只能看到一个 JS 和 CSS 文件,并且全部被压缩和缩小。
顺便说一句,我不使用预编译资源。但只是为了一笑,我执行了rake assets:precompiled
,你知道吗? ?debug_assets=1
现在显示所有 JS 和 CSS 文件,并且没有一个是 404 的。
所以我想您可能会有的问题是,“为什么不只使用预编译资源而不用担心延迟加载会丢失资源?” 好点。回答:我只是想确保我了解我在做什么、正在发生什么以及我正在正确地做事。
application.rb:
config.assets.enabled = true
config.assets.version = '1.2'
生产.rb:
config.assets.compress = true
config.assets.compile = true
config.assets.digest = true
config.assets.js_compressor = :uglifier
config.assets.css_compressor = :scss
开发.rb:
config.assets.compress = false
# I keep this off during development because I want
# to make sure the compression isn't breaking my JS
config.assets.debug = false
This question is similar to Why do I get “no route matches” for requests to the asset pipeline?.
I have a rails 3.0 application that I upgraded to 3.1 and converted to use the new asset pipeline (thanks to RailsCasts #282 and #279).
In production mode, I'm seeing the application-<digest>.js and application-<digest>.css. Great! And if I look at the source of those files, I see they are compressed. Yee-haw! So that means the asset pipeline is working, right?
However, if I add ?debug_assets=1
to the URL so that I may view individual files, some of them are producing ActionController::RoutingError (No route matches [GET] "/assets/<filename>-<digest>.js")
, and same goes for some CSS files. But not all, just some, and I can't figure out what makes some files do this and others not.
I've cleared out tmp/cache/* and restarted Passenger. I've bumped config.assets.version
. I've restarted memcached. None of these seem to resolve it. But what's odd is this only comes up when I'm using ?debug_assets=1
in the URL; without it, I see just one JS and CSS file, all compressed and minified.
I don't use precompiled assets, by the way. But just for grins, I performed a rake assets:precompiled
, and whaddya know? The ?debug_assets=1
now shows all JS and CSS files, and none of them are 404'd.
So I guess the question you might have is, "Why not just use precompiled assets and not worry about missing assets from lazy load?" Good point. Answer: I just like to make sure I understand what I am doing, what's happening, and that I'm doing things correctly.
application.rb:
config.assets.enabled = true
config.assets.version = '1.2'
production.rb:
config.assets.compress = true
config.assets.compile = true
config.assets.digest = true
config.assets.js_compressor = :uglifier
config.assets.css_compressor = :scss
development.rb:
config.assets.compress = false
# I keep this off during development because I want
# to make sure the compression isn't breaking my JS
config.assets.debug = false
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您预编译资产并将编译设置为 false,则会禁用调试,因为您已告诉 Rails 根本不使用 Sprockets,但假设 nginx 可以根据资产管道清单中的映射来提供这些文件。
当编译为 true 时(就像您一样),如果文件丢失(没有预编译的情况),对这些资产的请求(以及调试请求)将被发送回 Sprockets 进行处理。
我本以为 Sprockets 会为每个摘要名称提供单独的文件。这种行为对我来说听起来有问题,尽管我不认为它打算在生产中使用调试。
If you precompile your assets and set compile to false, debug is disabled because you've told Rails to not use Sprockets at all, but assumes that the files can be served by nginx based on mappings in the asset pipeline manifest.
When compile is true (like you have) then requests for these assets (and the debug request) are sent back to Sprockets to be processed if the files are missing (which without being precompiled is the case).
I would have assumed Sprockets would serve the individual files for each digested name. This behavior sounds buggy to me, although I don't think it is intended to use debug in production anyway.