Skip to content

Commit

Permalink
Only pass :request_id option to Refresh Stream (#603)
Browse files Browse the repository at this point in the history
When rendering `<turbo-stream>` elements for broadcasting, **omit** the
`:request_id` partial-local variable.

When rendered partials utilize [Action View strict locals][], the extra
variable raises an error:

```
unknown local :request_id
```

By omitting the `:request_id` from the
`Turbo::Broadcastable#broadcast_rendering_with_defaults`, it isn't
passed as part of partial rendering.

Likewise, since `broadcast_refresh` never renders a partial, but instead
builds an empty `<turbo-stream action="refresh"
request-id="..."></turbo-stream>` element, the `:request_id` local
variable isn't necessary.

[Action view strict locals]: https://guides.rubyonrails.org/action_view_overview.html#strict-locals
  • Loading branch information
seanpdoyle authored Sep 16, 2024
1 parent dfabcf7 commit 33019f3
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/models/concerns/turbo/broadcastable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ def broadcast_rendering_with_defaults(options)
options.tap do |o|
# Add the current instance into the locals with the element name (which is the un-namespaced name)
# as the key. This parallels how the ActionView::ObjectRenderer would create a local variable.
o[:locals] = (o[:locals] || {}).reverse_merge!(model_name.element.to_sym => self, request_id: Turbo.current_request_id).compact
o[:locals] = (o[:locals] || {}).reverse_merge!(model_name.element.to_sym => self).compact

if o[:html] || o[:partial]
return o
Expand Down
4 changes: 3 additions & 1 deletion test/dummy/app/views/messages/_message.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
<p><%= message %></p>
<%# locals: (message:, profile: nil) %>

<p><%= message %></p>

0 comments on commit 33019f3

Please sign in to comment.