I’m trying to set up my first turbo-stream broadcasts_to
from a Comment
model.
<code>class Comment < AbstractModel
belongs_to :target, polymorphic: true
after_create :notify_users
broadcasts_to(->(comment) { [comment.target, :comments] },
inserts_by: :prepend, partial: "comments/comment")
<code>class Comment < AbstractModel
belongs_to :target, polymorphic: true
belongs_to :user
after_create :notify_users
broadcasts_to(->(comment) { [comment.target, :comments] },
inserts_by: :prepend, partial: "comments/comment")
</code>
class Comment < AbstractModel
belongs_to :target, polymorphic: true
belongs_to :user
after_create :notify_users
broadcasts_to(->(comment) { [comment.target, :comments] },
inserts_by: :prepend, partial: "comments/comment")
On create, update or destroy, it’s not finding the partial supplied in the args.
<code>Started PATCH "/comments/195463?q=1oNBT" for ::1 at 2024-05-10 10:39:20 -0700
Processing by CommentsController#update as TURBO_STREAM
[ActiveJob] Enqueued Turbo::Streams::ActionBroadcastJob (Job ID: e1c46c84-d13c-4562-a2c1-9b9ea85b7a22) to SolidQueue(default) with arguments: "Z2lkOi8vbXVzaHJvb20tb2JzZXJ2ZXIvT2JzZXJ2YXRpb24vNTE1NzEz:comments", {:action=>:replace, :target=>#<GlobalID:0x0000000118d8b468 @uri=#<URI::GID gid://mushroom-observer/Comment/195463>>, :targets=>nil, :attributes=>{}, :partial=>"comments/comment", :locals=>{:comment=>#<GlobalID:0x0000000118d8b058 @uri=#<URI::GID gid://mushroom-observer/Comment/195463>>, :request_id=>"6909dbb3-b936-4ab9-b4e6-89392e62d7c5"}}
No template found for CommentsController#update, rendering head :no_content
<code>Started PATCH "/comments/195463?q=1oNBT" for ::1 at 2024-05-10 10:39:20 -0700
Processing by CommentsController#update as TURBO_STREAM
[ActiveJob] Enqueued Turbo::Streams::ActionBroadcastJob (Job ID: e1c46c84-d13c-4562-a2c1-9b9ea85b7a22) to SolidQueue(default) with arguments: "Z2lkOi8vbXVzaHJvb20tb2JzZXJ2ZXIvT2JzZXJ2YXRpb24vNTE1NzEz:comments", {:action=>:replace, :target=>#<GlobalID:0x0000000118d8b468 @uri=#<URI::GID gid://mushroom-observer/Comment/195463>>, :targets=>nil, :attributes=>{}, :partial=>"comments/comment", :locals=>{:comment=>#<GlobalID:0x0000000118d8b058 @uri=#<URI::GID gid://mushroom-observer/Comment/195463>>, :request_id=>"6909dbb3-b936-4ab9-b4e6-89392e62d7c5"}}
No template found for CommentsController#update, rendering head :no_content
</code>
Started PATCH "/comments/195463?q=1oNBT" for ::1 at 2024-05-10 10:39:20 -0700
Processing by CommentsController#update as TURBO_STREAM
[ActiveJob] Enqueued Turbo::Streams::ActionBroadcastJob (Job ID: e1c46c84-d13c-4562-a2c1-9b9ea85b7a22) to SolidQueue(default) with arguments: "Z2lkOi8vbXVzaHJvb20tb2JzZXJ2ZXIvT2JzZXJ2YXRpb24vNTE1NzEz:comments", {:action=>:replace, :target=>#<GlobalID:0x0000000118d8b468 @uri=#<URI::GID gid://mushroom-observer/Comment/195463>>, :targets=>nil, :attributes=>{}, :partial=>"comments/comment", :locals=>{:comment=>#<GlobalID:0x0000000118d8b058 @uri=#<URI::GID gid://mushroom-observer/Comment/195463>>, :request_id=>"6909dbb3-b936-4ab9-b4e6-89392e62d7c5"}}
No template found for CommentsController#update, rendering head :no_content
Why not? The partial is in comments/comment.erb
.
My understanding is that with broadcasts_to
and a partial
arg, I should not supply turbo_stream
responses in the controller, Rails will figure it out.
<code># comments_controller.rb
# ...update the comment if validated, then:
# turbo_stream: turbo_stream.prepend(
# :comments, partial: "comments/comment", locals: { comment: @comment }
<code># comments_controller.rb
def update
# ...update the comment if validated, then:
respond_to do |format|
format.turbo_stream # do
# render(
# turbo_stream: turbo_stream.prepend(
# :comments, partial: "comments/comment", locals: { comment: @comment }
# )
# )
# end
format.html do
redirect_to(@target)
end
end
end
</code>
# comments_controller.rb
def update
# ...update the comment if validated, then:
respond_to do |format|
format.turbo_stream # do
# render(
# turbo_stream: turbo_stream.prepend(
# :comments, partial: "comments/comment", locals: { comment: @comment }
# )
# )
# end
format.html do
redirect_to(@target)
end
end
end
If i do render turbo_stream in the controller response, the comment updates for the current user, but does not stream to other users, even though the turbo-cable-stream-source
is connected
on both browsers.
The view partial is streaming from the correct stream, I believe:
<code><%= turbo_stream_from([target, :comments], :comments) %>
<code><%= turbo_stream_from([target, :comments], :comments) %>
</code>
<%= turbo_stream_from([target, :comments], :comments) %>
This is a mature Rails 7.1.3/Ruby 3.3.0 app where Turbo, Stimulus, importmaps, and SolidQueue are already running ok. I’ve added redis
for ActionCable and i’m running a puma server in dev. Everything seems ok with the websocket connection and ActiveJob.
url: redis://localhost:6379/1
<code># cable.yml
development:
adapter: redis
url: redis://localhost:6379/1
</code>
# cable.yml
development:
adapter: redis
url: redis://localhost:6379/1
What am I missing?