How to send page updates with [Hotwire](https://hotwire.dev) using a specific partial.
How to send page updates with Hotwire using a specific partial.
When broadcasting, the rendering engine expects a partial to render. This is nice anyway because you can use the partial in the rendered HTML page. By default it uses the partial for the model doing the broadcast. But it's easily overridden.
Setup the main view with
turbo_stream_from and render the partial that will be changed by the broadcast in a container with an id,
= turbo_stream_from @profile
== render 'verify_id', profile: @profile
/ SUCCEED Verification
- if profile.result == RESULTS.succeed
| That worked!
/ FAILED Verification
- if profile.result == RESULTS.failed
| Oops that failed :(
Now, from a background job, response to a webhook, or where ever; you can broadcast the update.
profile.broadcast_action :update, target: "verification_status", partial: "profile/status"
Make sure to include
Turbo::Broadcastable in the model.
class Profile < ApplicationRecord
To make sure whatever should broadcast is broadcasting you can use
class Hooks::ProfileController < ActionDispatch::IntegrationTest
test "should handle callback" do
profile = create(:profile)
assert_broadcasts stream_name_from(profile), 1 do