Skip to content

Instantly share code, notes, and snippets.

@hungmi
Created May 4, 2024 09:13
Show Gist options
  • Save hungmi/b26eda8687ab1a70f184460d88e73b50 to your computer and use it in GitHub Desktop.
Save hungmi/b26eda8687ab1a70f184460d88e73b50 to your computer and use it in GitHub Desktop.
Turbo broadcast_*_to via ActionCable
<!-- app/views/hello.html -->
<h1>Hello!</h1>
# app/controllers/hello_controller.rb
class HelloController < ApplicationController
def create
Turbo::StreamsChannel.broadcast_append_to(
@resource.id,
:hello,
target: 'hello-turbo-frame',
template: 'hello',
layout: false
)
end
end
<!-- app/views/resources/show.html.erb -->
<%= turbo_stream_from @resource.id, :hello %>
<turbo-frame id="hello-turbo-frame"></turbo-frame>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment