Rails hotwired 中 turbo frame 的请求仍然渲染 layout,这是一个 Bug 吗?

laocainiao · 2022年02月14日 · 最后由 laocainiao 回复于 2022年02月14日 · 402 次阅读

使用了 hotwired-rails 后,对于 turbo frame 类型的请求,如:

<%= turbo_frame_tag "edit_dialog" do %>
<%= link_to "Edit",  xxxx_path(user) %>
<% end %>

或者 <%= link_to "Edit", xxxx_path(user), data: {turbo_frame: "edit_dialog"}%>

我希望它的响应中不要渲染 layout, 看了源码,发现也的确有这个功能,但是实际上我在项目中必须我手动在控制器中显示指定:

respond_to do |format|      
      format.html { render layout: false if turbo_frame_request? }
end

所以我想问一下这是一个 bug 吗?

发现了这个讨论 https://github.com/hotwired/turbo-rails/issues/60,人家好像是没有觉得这是一个 Bug,没有搞明白。

这个不算 BUG.

这个讨论里已经说明白了。虽然 hotwired-rails 这个 gem 预先设置了 layout -> { false if turbo_frame_request? }, 但是你自己使用了 layout 方法覆盖了它的行为。所以你改一下应该就可以了:

class TestController < ApplicationController
  layout -> { turbo_frame_request? ? false : "custom" }
end

对,我的确在 application controller 中使用了自己的 layout 方法。谢谢了。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号