为什么 Rails 在布局中呈现 RJS 模板?
我需要为使用 active_scaffold
gem 的控制器执行此操作。我们有一个看起来像这样的控制器:
class Admin::UsersController < ApplicationController
layout 'admin'
active_scaffold :users do |config|
config.search.columns = [:first_name, :last_name]
end
end
当我们使用 Rails 2.3.10 时,它工作得很好,但我们正在升级到 Rails 3.0.10。作为升级的一部分,我必须升级 active_scaffold
(当前从 git://github.com/activescaffold/active_scaffold< 的
rails-3.0
分支安装) /code>) 兼容。我们遇到的一个问题是搜索表格不起作用。我会在日志文件中看到:
Rendered <snip>/gems/active_scaffold-25b3d724f35b/frontends/default/views/list.js.rjs within layouts/admin (923.5ms)
请注意,它正在使用控制器中指定的布局渲染 RJS 模板。对我来说,这似乎是不合理的默认。 RJS 模板不应该在默认情况下渲染布局吗?不管怎样,我这样修复了它:
class Admin::UsersController < ApplicationController
layout :admin_layout
private
def admin_layout
respond_to do |format|
format.js { false }
format.html { 'admin' }
end
end
end
修复了搜索和分页的问题。 (RJS 模板现在无需布局即可呈现,因此浏览器可以执行生成的 Javascript)。我想我的问题是,为什么我必须告诉 Rails 它不应该使用布局渲染 RJS 模板?还有更好的解决方案吗?对我来说,这感觉像是太多的黑客攻击(糟糕的黑客攻击——将来会崩溃的黑客攻击)。
I need to do this for a controller which uses the active_scaffold
gem. We have a controller that looked something like this:
class Admin::UsersController < ApplicationController
layout 'admin'
active_scaffold :users do |config|
config.search.columns = [:first_name, :last_name]
end
end
That worked great when we were on Rails 2.3.10, but we're upgrading to Rails 3.0.10. As part of the upgrade, I had to upgrade active_scaffold
(currently installed from the rails-3.0
branch of git://github.com/activescaffold/active_scaffold
) to be compatible. One problem we were having is that searching the table wasn't working. I would see in my log files:
Rendered <snip>/gems/active_scaffold-25b3d724f35b/frontends/default/views/list.js.rjs within layouts/admin (923.5ms)
Notice that it's rendering the RJS template with the layout specified in the controller. That seems like an unreasonable default to me. Shouldn't RJS templates render without a layout by default? Anyway, I fixed it as such:
class Admin::UsersController < ApplicationController
layout :admin_layout
private
def admin_layout
respond_to do |format|
format.js { false }
format.html { 'admin' }
end
end
end
That fixes the issues with search and pagination. (The RJS template is now rendered without a layout, so the browser can execute the resulting Javascript). I guess my question is, why do I have to tell Rails that it shouldn't render RJS templates with layouts? And is there a better solution? This feels like too much of a hack to me (the bad kind of hack---the kind of hack that will break in the future).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我明白了。 @numbers1311407 在我的问题下的评论让我检查了布局模板的名称。它是
layouts/admin.haml
。在 Rails 2 中,该布局仅针对 HTML 请求进行呈现,但在 Rails 3 中,它适用于所有请求(因为它没有指定格式类型)。我将其重命名为layouts/admin.html.haml
并且它可以在我的控制器中使用一个简单的layout 'admin'
(而不是我想出的 hack)在我的问题中)。那么问题的答案是“为什么 Rails 在布局中呈现 RJS 模板?”布局文件的命名使其适用于所有格式。
Okay, I figured it out. @numbers1311407's comment under my question led me to check the name of the layout template. It was
layouts/admin.haml
. With Rails 2, that layout was only rendering for HTML requests, but with Rails 3 it applies to all requests (because it doesn't specify a format type). I renamed it tolayouts/admin.html.haml
and it works with a simplelayout 'admin'
in my controller (as opposed to the hack that I had come up with in my question).So the answer to the question, "Why does Rails render RJS templates within a layout?" is that the layout file was named such that it applies to all formats.
回答您的问题:
1. Rails 渲染器针对 JS 格式进行布局并没有什么神奇之处。这是因为 Rails 默认使用任何模板渲染布局,除非您明确告诉要避免它。您只需查看文件
actionpack/lib/action_controller/metal/renderers.rb
中的 Rails 源代码即可查看 :js 渲染器。2.尝试使用:
Answer your quastions:
1. There is no magic that Rails renderers layout in for JS format. That's bacause it is default to Rails to render layout with any template unless you explicitly tell to avoid it. You can just look into Rails sources in file:
actionpack/lib/action_controller/metal/renderers.rb
to see :js renderer.2.Try to use: