我想根据ActiveRecord的作用域路由将其更新广播到不同的部分。我怎么能这么做?
例句:我有一个has_many :comments的贴子模型。可以更新注释,我们在以下文件夹中为comment提供了两个部分,以便使用不同的模板显示它们:
<%= turbo_stream_from comment %>
<%= turbo_frame_tag dom_id(comment) do %>
//one way of displaying the comment
<% end %><%= turbo_stream_from comment %>
<%= turbo_frame_tag dom_id(comment) do %>
//another way of displaying the comment
<% end %>每次我更新唱片时,都会用涡轮流广播。
class Comment < ApplicationRecord
broadcasts_to :post
end问题是,广播的结果是在整个应用程序中使用/views/comments/_comment.html.erb。当视图位于特定范围内时,我需要一种广播和重新呈现注释的方法,以及在没有定义范围时使用未限定范围的部分,这可能吗?
发布于 2022-02-07 17:57:54
我在宝石里找到了答案..。这可以通过将访问者注释的dom_id更改为"#{dom_id(comment)}_visitor"并在broadcast_replace_to中显式传递部分来执行。
<%= turbo_stream_from comment %>
<%= turbo_frame_tag "#{dom_id(comment)}_visitor" do %>
//another way of displaying the comment
<% end %>class Comment < ApplicationRecord
after_update_commit do
broadcast_replace_to self
broadcast_replace_to self, target: "#{dom_id(self)}_visitor", partial: 'visitor/comments/comment'
end
endhttps://stackoverflow.com/questions/71020508
复制相似问题