stimulusreflex / stimulus_reflex

Build reactive applications with the Rails tooling you already know and love.
https://docs.stimulusreflex.com
MIT License
2.28k stars 172 forks source link

Is there a way to renter a partial and update the specific DOM element #209

Closed henrydjacob closed 4 years ago

ghost commented 4 years ago

Hey @henrydjacob

Yes, you can use CableReady for that. In your controller, you will have to include CableReady::Broadcaster

class MessagesController < ApplicationController
  include CableReady::Broadcaster
# POST /messages
  def create
    @message = Message.create(message_params)

    cable_ready["timeline:#{@message.project.id}"].insert_adjacent_html(
      selector: "#timeline",
      position: "afterbegin",
      html: render_to_string(partial: 'message', locals: { message: @message })
    )
    cable_ready.broadcast
  end

I hope this helps. You can read more about it here: https://cableready.stimulusreflex.com/

henrydjacob commented 4 years ago

This is great, can we also subscribe to a specific room and send these updates only to those users who are subscribed

leastbad commented 4 years ago

Exactly! You get the 💡 now.