ruby-on-rails Rails Turbo如何在单击时呈现部分?

yqlxgs2m  于 7个月前  发布在  Ruby
关注(0)|答案(1)|浏览(73)

我有一个Ruby on Rails应用程序,它的弹出窗口当前设置为通过Turbo Toggle显示和隐藏。然而,我注意到这种方法会影响页面加载速度。
为了减少父页面的加载时间,我考虑只在单击时呈现选项卡。
第一个月

q35jwt9p

q35jwt9p1#

好吧,让我们来看看什么对你有帮助。基本上你有三个选择
1.帧加载更快,性能更好
1.使用Turbo-Frames进行导航
1.使用Stimulus(您已经在使用它)
无论哪种方式,您都必须提供一个单独的方法和视图来呈现customers-part - something like 'customer/:id'
为了让前两个选项工作,你必须确保你的customer-layout(子布局)包含turbo-frames标签。使用Rails你可以做:

<%= turbo_frame_tag(@customer) do %>
  <%= #all of your customer-stuff %>
<% end %>

字符串
这使得

<turbo-frame id="customer_1">all of your customer-stuff</turbo-frame>


对于第一个选项,帧的快速加载是这样完成的:

<%= turbo_frame_tag @customer, src: customer_path(@customer) %>


第二,只是

<%= turbo_frame_tag @customer %>


只要确保您创建turbe-frame标记的方式与子布局中的相同,并且替换应该自动发生。
对于第三个选项,你可以获取子页面/customer/:id并替换div。看看https://stimulus.hotwired.dev/handbook/working-with-external-resources的例子。

相关问题