Module: Turbo::Streams::ActionHelper

Included in:
Broadcasts, TagBuilder
Defined in:
app/helpers/turbo/streams/action_helper.rb

Instance Method Summary collapse

Instance Method Details

#turbo_stream_action_tag(action, target:, template: nil) ⇒ Object

Creates a ‘turbo-stream` tag according to the passed parameters. Examples:

turbo_stream_action_tag "remove", target: "message_1"
# => <turbo-stream action="remove" target="message_1"></turbo-stream>

turbo_stream_action_tag "replace", target: "message_1", template: %(<div id="message_1">Hello!</div>)
# => <turbo-stream action="replace" target="message_1"><template><div id="message_1">Hello!</div></template></turbo-stream>


9
10
11
12
13
14
# File 'app/helpers/turbo/streams/action_helper.rb', line 9

def turbo_stream_action_tag(action, target:, template: nil)
  target   = convert_to_turbo_stream_dom_id(target)
  template = template ? "<template>#{template}</template>" : ""

  %(<turbo-stream action="#{action}" target="#{target}">#{template}</turbo-stream>).html_safe
end