将 Turbo 流更新为全局变量更新(Hotwire & Ruby On Rails)



我使用Ruby On Rails和Hotwire(Turbo(来动态更新我的页面,而用户必须重新加载。

我有一个全局变量,它是一个数组,我在页面上循环它。例如

<% $listNumberPlates.each { |item| %>
<%= item %>
<% } %>

现在,每当变量发生变化时,我想更新它(不重新加载(。为了更容易,我有一个更新变量的方法。

我试过使用。。。

<%= turbo_stream_from "listNumberPlatesStream" %>
<%= turbo_frame_tag "listNumberPlatesStream" do %>

但我不知道如何更新这些。任何帮助或建议都将不胜感激。非常感谢。

您可以使用类似的东西:

after_create_commit do
broadcast_append_to [commentable, :comments], target: "#{dom_id(parent || commentable)}_comments", partial: 'comments/comment_with_replies', locals: { current_user: user }
end
after_update_commit do
broadcast_replace_to self, partial: 'comments/comment', locals: { current_user: user }
end
after_destroy_commit do
broadcast_action_to self, action: :remove, target: "#{dom_id(self)}_with_comments"
end

在这种观点中,应该有这样的东西:

= turbo_stream_from @session, :comments
= tag.div id: "#{dom_id(@session)}_comments" do
= render partial: "comments/comments", collection: @session.root_comments, as: :comment, locals: { current_user: current_user }

并在github 中查看文档中的相关方法broadcast_action_to

最新更新