Last active
June 17, 2023 23:21
-
-
Save dpaluy/bb262a7b0296e23b2b0462282059a151 to your computer and use it in GitHub Desktop.
Turbo Stream Logs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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