Skip to content

Conversation

camertron
Copy link
Contributor

What are you trying to accomplish?

Fixes #2423

What approach did you choose and why?

ViewComponent::Base retains a reference to the view context's output buffer. When component templates are evaluated, they do so inside the context of ViewComponent::Base, and thus append to the retained @output_buffer instance. However, Rails creates a new output buffer when rendering partials, and temporarily reassigns its own @output_buffer instance variable. When rendering partials inside a view component, ViewComponent::Base forwards the block on to view_context.render, but since the block was created inside ViewComponent::Base, that's where it will be evaluated when called. This means the block will append to the original @output_buffer and not to the new one Rails creates for the partial. To mitigate the issue, this PR evaluates the block inside the view context with instance_exec, which causes the block to append to the correct buffer.

@camertron camertron marked this pull request as ready for review August 6, 2025 17:01
@camertron camertron requested a review from joelhawksley August 6, 2025 17:01
Copy link
Collaborator

@rmacklin rmacklin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@doits
Copy link

doits commented Aug 9, 2025

thank you, LGTM!

@camertron camertron merged commit a755f3c into main Aug 15, 2025
17 checks passed
@camertron camertron deleted the fix_partial_with_yield branch August 15, 2025 04:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Partials that yield result in double rendering bug
3 participants