Hotwire Discussion

Unable to append (or prepend) new content

I’ve been playing around with adding a Turbostreams into a project.

I have a page that displays all posts (post) with comments (comment) listed along side. Each post has_many comments.

I have them displayed something like this:

  <div class="post__comments--inner">
      <%= render '/comments/show', post: post, comment: %>


  <%= turbo_stream_from post, :comments %>
  <div class="comments__list">
    <%= render partial: "comments/comment", collection: post.comments %>
  <div class="comments__form">
    <%= turbo_frame_tag "new_comment", src:  new_post_comment_path(post), target: "_top" %>


  def create
    @comment = current_user.comments.create!(comment_params)
    respond_to do |format|
        format.turbo_stream do
          render turbo_stream: turbo_stream.append(:comments, partial: 'comments/comment',
                                                              locals: { comment: @comment })
        format.html { redirect_to }

When submit a new comment on any post it gets persisted into the database, and the network returns a 200 POST request with this content:

<turbo-stream action="append" target="comments"><template>  <div class="comment" id="comment_61">
    <div class="comment__user">
    <div class="comment__text">

But it does not actually show up anywhere.

I’m wondering if the fact that I have multiple lists of comments on the page is causing problems, target="comments" isn’t specific to one comment field.

That is exactly the issue here, one feature of turbo is the ids must be unique, im guessing that there are multiple models that have the comments conatiner on the same page. to solve this i would do this inside the container for each post

  <div class="comments__list"  id="<%= dom_id(post, :comments)  %>">
    <%= render partial: "comments/comment", collection: post.comments %>

this would yield comment containers with ids like


this way you would always have unique ids

you will also need to gain access to the post that the comment belongs to, to pass it to the partial

        format.turbo_stream do
          render turbo_stream: turbo_stream.append(dom_id(, :comments), partial: 'comments/comment',
                                                              locals: { comment: @comment })

Thanks, the adding the dom_id into the controller solves everything!

1 Like

great!. happy it worked. remember, turbo relies heavily on IDS and if the id’s aren’t unique then you face weird bugs n things. as a rule of thumb, whenever you try finding yourself replacing or using frame. it’s always preferred to use dom_id it’s a very good little helper available to you.

happy coding!.

Yup. One of the lessons I keep learning when it comes to JavaScript is an invalid DOM will either give you no result or intermittently wrong results without explaining anything to you. If your script fails, check the HTML with a validator, like the one from