Hello!
I am indeed returning a stream format.
My comments#create
is
def create
@comment = @post.comments.build(comments_params)
respond_to do |format|
if @comment.save
format.turbo_stream
format.html { redirect_to @post, notice: 'Comment was successfully created.' }
format.json { render :show, status: :created, location: @comment }
else
[...error handling]
end
end
end
with the partial create.turbo_stream
itself being
<% if @comment.errors.present? %>
[...some error handling]
<% else %>
<%= comment_notice_stream(message: :create, status: 'green') %> # a helper to display a notice
<%= turbo_stream.replace 'new_comment' do %>
<%= turbo_frame_tag :new_comment %>
<% end %>
<%= turbo_stream.append "#{dom_id(@post)}_comments", partial: 'comment', locals: { comment: @comment } %>
<%= turbo_stream.replace 'total_comments' do %>
<%= turbo_frame_tag :total_comments, class: 'mt-2 text-lg text-2xl mt-2 tex-gray-50' do %>
<%= @post.comments.size %> comments
<% end %>
<% end %>
<% end %>
When I rename the create.turbo_stream
file and/or comment out the format.turbo_stream
, and then try creating a comment from a different browser window, the comment gets created but does not show on the page without a reload (which is kind of expected since I removed the turbo_stream
) but does not appear on the other browser window either, where I would expect it to be broadcasted. This means that I am probably not broadcasting as I should.
I still get in the console [ActionCable] Broadcasting to [...some string]:comments:.....
Basically what I did is follow this article to get me started with a “spa-like” version of the blog and then tried to tweak it to add some actioncable/broadcasting for the posts/comments.
Thanks again for your help!