Turbo Broadcastables & Helpers #806
-
Hi! Before anything, thanks for your work! I'm trying I'm using Phlex in a rails app and currently facing an issue when trying to broadcast from the model (https://www.rubydoc.info/gems/turbo-rails/Turbo/Broadcastable) Broadcasting the broadcast_append_to(channel_id, html: Components::ChatMessage.new(self).call, target: "thread_#{thread.id}_messages")
# undefined method `class_names' for nil (NoMethodError) I'm using a workaround to render the component as if it would be in a controller context: def render_component(component)
ApplicationController.render(component, layout: false)
end
broadcast_append_to(channel_id, html: render_component(Components::ChatMessage.new(self)), target: "thread_#{thread.id}_messages") Is there a proper way to do it? Couldn't find an answer in the docs or the discussions. EDIT: using the v2 |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 8 replies
-
I haven't tried it, but I wonder if you could use |
Beta Was this translation helpful? Give feedback.
-
Testing in the console: ApplicationController.render_to_string(Components::ChatMessage.new(Message.first))
# nil ApplicationController.render_to_string(Components::ChatMessage.new(Message.first).call)
# `view_template': undefined method `class_names' for nil (NoMethodError) The following alternatives work: Components::ChatMessage.new(Message.first).render_in(ActionController::Base.new.view_context) ApplicationController.new.view_context.render(Components::ChatMessage.new(Message.first)) I got the ideas from ViewComponents here: ViewComponent/view_component#1106 |
Beta Was this translation helpful? Give feedback.
-
try to use the broadcast_replace_to(user, :message, target: "message", renderable: MessageComponent.new) PS: I just implemented here and it worked perfectly well. |
Beta Was this translation helpful? Give feedback.
try to use the
:renderable
following the example provided by the turbo rails doc:PS: I just implemented here and it worked perfectly well.