Broadcasting to nested turbo_frame_tag

Hey!
So I found a way to make it work.

In posts/show, it was a matter of putting the stream inside the turbo_frame:

<%= turbo_frame_tag dom_id(@post) do %>
[...]
    <%= turbo_stream_from @post, :comments %>
    <div class="w-full bg-white rounded-md p-4 justify-start mt-1">
      <div class="w-full pt-2">
        <turbo-frame id="total_comments" class="mt-2 text-lg text-2xl mt-2 tex-gray-50">
          <%= @post.comments.size %> comments
        </turbo-frame>
        <%= turbo_frame_tag "#{dom_id(@post)}_comments" do %>
          <%= render @post.comments %>
        <% end %>

        <div class="w-full mb-5 mt-5">
        <%= turbo_frame_tag :new_comment %>
      </div>
      <%= link_to 'New Comment', new_post_comment_path(@post), data: { turbo_frame: 'new_comment' }, class: 'bg-blue-500 text-white text-center p-2 rounded-md' %>
      </div>
    </div>
[...]
<% end %>

That makes creating a comment work.

And to delete a comment in both windows I needed to add a stream at the beginning of the partial:

<%= turbo_stream_from comment %>
<%= turbo_frame_tag dom_id comment do %>
 [...content]
<% end %>

the only thing left to solve is how to update in both windows the count when broadcasting a new comment:

in posts/show:

        <turbo-frame id="total_comments" class="mt-2 text-lg text-2xl mt-2 tex-gray-50">
          <%= @post.comments.size %> comments
        </turbo-frame>

If you have any idea, let me know :slight_smile:
Have a good day.

1 Like