instance method turbo_stream_from

Ruby on Rails 2.0.23

Since v1.3.3 Last seen in v2.0.23

Available in: v1.3.3 v1.4.0 v1.5.0 v2.0.23

Signature

turbo_stream_from(*streamables, **attributes)

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

Parameters

streamables rest
attributes keyrest
Source
# 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

Defined in app/helpers/turbo/streams_helper.rb line 56 · View on GitHub · Improve this page · Find usages on GitHub

Defined in Turbo::StreamsHelper

Type at least 2 characters to search.

↑↓ navigate · open · esc close