Skip to content

Instantly share code, notes, and snippets.

@tgaff
Last active February 8, 2023 03:19
Show Gist options
  • Save tgaff/6bcda98f586c0f9c0a3b2f56db1f4c27 to your computer and use it in GitHub Desktop.
Save tgaff/6bcda98f586c0f9c0a3b2f56db1f4c27 to your computer and use it in GitHub Desktop.
multiple turbo streams

There are two ways to render multiple turbo streams:

  1. via the controller rendering an array of streams
  2. via a view file that contains multiple stream templates

The other two files on this gist demonstrate both methods.

DHH considers the first one bad.

I kinda like the first one because I think it makes it a little more obvious what the controller code was responsible for passing to where.

respond_to do |format|
format.turbo_stream do
render turbo_stream: [
turbo_stream.replace(:flash, partial: "layouts/flash", locals: { notice: "Message posted!" }),
turbo_stream.append(:messages, partial: "messages/message", locals: { message: message })
]
end
end
<%= turbo_stream.replace "flash" do %>
<%= render partial: 'layouts/flash', locals: { notice: "Message posted!" } %>
<% end %>
<%= turbo_stream.append "messages" do %>
<%= render partial: 'messages/messages', locals: { message: @message } %>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment