4

Relatively new to Stimulus and Hotwire and wanted to know,

"Is there a way to render two different partials for the same broadcasted object on two different pages?

I’m trying to render and broadcast an object on a page from one controller to another page from different controller with different styling.

Example If I print <%= render @live_room.room.questions %> in page live_room.html.erb and print <%= render @room.questions %> in show.html.erb

They both render the partial _question.html.erb

I would like render a different design depending on the page.

Has anyone faced this issue before or know how to solve this? I’d love to get some insight!

Cheers!

CoderTrack
  • 71
  • 5

1 Answers1

2

You can specify which partials to render. For example, after the cart model is updated a replacement is broadcasted to the badge_id and another one to cart. Each renders a unique partial.

class Cart
  ...

  after_update_commit do
    broadcast_replace_to 'badge',
                         partial: 'carts/badge',
                         locals: { cart: self },
                         target: "badge_#{id}"
  end

  after_update_commit do
    broadcast_replace_to 'cart',
                         partial: 'carts/active_cart',
                         locals: { cart: self }
  end
Thomas Van Holder
  • 1,137
  • 9
  • 12
  • NB with latest stimulus-rails, the cart object gets automatically sent to the partial. You won't need to declare 'locals: { cart: self }'. However, locals can be useful if you wanted to call a partial for a different object... partial: 'users/profile', 'locals: { user: user }' – jnicho02 Oct 12 '22 at 09:48