简体   繁体   中英

Hotwire: How to render two partials for a broadcasted object on to two different pages?

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM