Skip to content

Instantly share code, notes, and snippets.

@dpaluy
Last active June 17, 2023 23:21
Show Gist options
  • Save dpaluy/bb262a7b0296e23b2b0462282059a151 to your computer and use it in GitHub Desktop.
Save dpaluy/bb262a7b0296e23b2b0462282059a151 to your computer and use it in GitHub Desktop.
Turbo Stream Logs
# app/model/users_mixins/actions.rb
module UserMixins: :Action
def console_log (message)
message = #{message}"
# render app/views/shared/actions/console_log partial
html = ApplicationController.render(
partial: partial_path, locals: { message: message },
)
Turbo: :StreamsChannel. broadcast_append_to(
self, target: action_target, html: html.html_safe
)
end
end
__END__
# some_controller.rb
def show
current_user. console_log "Hello world"
end
# app/helpers/turbo_streams_actions_helper.rb
def console_log(message)
turbo_stream_action_tag :console_log, message: message
end
# app/views/shared/actions/_console_log.html.erb
# <%= turbo_stream. console_log(message) %>
# turbo_streams.js
StreamActions.console_log = function() {
console.log(this.getAttribute("message"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment