ruby-on-railsruby-on-rails-7hotwire-railsturbo-frames

How to render partials recursively with turbo frames and stream


Let me explain my setup. I have a Comment model, and I've utilized the same table to include both comments and their respective replies. Below is how I've implemented it

    class Comment < ApplicationRecord
      belongs_to :post
      belongs_to :user
      has_many :likes, as: :likeable
      scope :ordered, -> { order(id: :desc) }
      belongs_to  :parent, class_name: 'Comment', optional: true
      has_many    :replies, class_name: 'Comment', foreign_key: :parent_id, dependent: :destroy
    
      validates :body, presence: true
    end

Solution

  •  - I made few changes to make it work
    ```
    <% if @comment.parent_id.nil? %>
      <%= turbo_stream.prepend "comments" do %>
        <%= render @comment %>
      <% end %>
    <% else %>
      <%= turbo_stream.after dom_id(@comment.parent) do %>
        <div class="ml-4">
          <%= render @comment %>
        </div>
      <% end %>
    <% end %>
    ```
     - now i can have comments rendered recursively