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:
posts/_show.html.erb
<div class="post__comments--inner">
<%= render '/comments/show', post: post, comment: Comment.new %>
</div>
comments/_show.html.erb
<%= turbo_stream_from post, :comments %>
<div class="comments__list">
<%= render partial: "comments/comment", collection: post.comments %>
</div>
<div class="comments__form">
<%= turbo_frame_tag "new_comment", src: new_post_comment_path(post), target: "_top" %>
</div>
comments_controller.rb
def create
@comment = current_user.comments.create!(comment_params)
respond_to do |format|
if @comment.save
format.turbo_stream do
render turbo_stream: turbo_stream.append(:comments, partial: 'comments/comment',
locals: { comment: @comment })
end
format.html { redirect_to @comment.post.place }
end
end
end
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">
me
</div>
</div>
<div class="comment__text">
comment
</div>
</div>
</template></turbo-stream>
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.