Method: Turbo::StreamsHelper#turbo_stream_from
- Defined in:
- app/helpers/turbo/streams_helper.rb
permalink #turbo_stream_from(*streamables, **attributes) ⇒ Object
Used in the view to create a subscription to a stream identified by the streamables
running over the Turbo::StreamsChannel
. The stream name being generated is safe to embed in the HTML sent to a user without fear of tampering, as it is signed using Turbo.signed_stream_verifier
. Example:
# app/views/entries/index.html.erb
<%= turbo_stream_from Current.account, :entries %>
<div id="entries">New entries will be appended to this target</div>
The example above will process all turbo streams sent to a stream name like account:5:entries
(when Current.account.id = 5). Updates to this stream can be sent like entry.broadcast_append_to entry.account, :entries, target: "entries"
.
Custom channel class name can be passed using :channel
option (either as a String or a class name):
<%= turbo_stream_from "room", channel: RoomChannel %>
It is also possible to pass additional parameters to the channel by passing them through ‘data` attributes:
<%= turbo_stream_from "room", channel: RoomChannel, data: {room_name: "room #1"} %>
Raises an ArgumentError
if all streamables are blank
<%= turbo_stream_from("") %> # => ArgumentError: streamables can't be blank
<%= turbo_stream_from("", nil) %> # => ArgumentError: streamables can't be blank
56 57 58 59 60 61 62 |
# File 'app/helpers/turbo/streams_helper.rb', line 56 def turbo_stream_from(*streamables, **attributes) raise ArgumentError, "streamables can't be blank" unless streamables.any?(&:present?) attributes[:channel] = attributes[:channel]&.to_s || "Turbo::StreamsChannel" attributes[:"signed-stream-name"] = Turbo::StreamsChannel.signed_stream_name(streamables) tag.turbo_cable_stream_source(**attributes) end |