DEV Community

Discussion on: Real-Time Partial Updates in Ruby on Rails using hotwire-rails

Collapse
 
anthonyfrancis profile image
Anthony Francis • Edited

Is Hotwire able to broadcast to two different model views? I'm having some trouble with that :/

Collapse
 
mikerogers0 profile image
Mike Rogers ✈️ • Edited

Yeah it can! Post your code up :)

Collapse
 
anthonyfrancis profile image
Anthony Francis

So I've figured out how to broadcast/render to two different model views, but I'm trying to broadcast/render a different partial/design for the view.

Whenever Hotwire broadcast/renders an object, it looks for the original partial, in my case questions/question through id="questions".

When broadcasting a new object to a view, the object gets broadcasted from questions/question and only renders the desired partial questions/question_live once the page has been refreshed, or if the questions/question_live is already on the page.

Example code

<tbody id="questions">
<%= render collection: @questions, partial: "questions/question_live", as: :question %>
</tbody>

Do you know if there is a way to stop Hotwire from rendering the id="questions" from partial questions/question and render from questions/question_live?

Thread Thread
 
anthonyfrancis profile image
Anthony Francis

Figured it out.

Question model

broadcasts_to ->(question) { :rooms }
after_create_commit ->(question) { broadcast_append_to :live_rooms, partial: "questions/question_live" }
after_update_commit ->(question) { broadcast_replace_to :live_rooms, partial: "questions/question_live }
after_destroy_commit ->(question) { broadcast_remove_to :live_rooms }
Enter fullscreen mode Exit fullscreen mode

live_room show view
<%= turbo_stream_from :live_rooms %>
...
code...
...

room show view
<%= turbo_stream_from :rooms %>
...
code...
...