Module: Turbo::Streams::ActionHelper

Includes:
ActionView::Helpers::TagHelper
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: nil, targets: nil, template: nil, **attributes) ⇒ 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>

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


14
15
16
17
18
19
20
21
22
23
24
# File 'app/helpers/turbo/streams/action_helper.rb', line 14

def turbo_stream_action_tag(action, target: nil, targets: nil, template: nil, **attributes)
  template = action.to_sym == :remove ? "" : tag.template(template.to_s.html_safe)

  if target = convert_to_turbo_stream_dom_id(target)
    tag.turbo_stream(template, **attributes.merge(action: action, target: target))
  elsif targets = convert_to_turbo_stream_dom_id(targets, include_selector: true)
    tag.turbo_stream(template, **attributes.merge(action: action, targets: targets))
  else
    tag.turbo_stream(template, **attributes.merge(action: action))
  end
end