Skip to content

Yielded content gets insertet at wrong location when used with Rails' form helpers#2617

Closed
doits wants to merge 1 commit into
ViewComponent:mainfrom
doits:outside_label_bug
Closed

Yielded content gets insertet at wrong location when used with Rails' form helpers#2617
doits wants to merge 1 commit into
ViewComponent:mainfrom
doits:outside_label_bug

Conversation

@doits
Copy link
Copy Markdown

@doits doits commented Apr 24, 2026

When passing the from helper to a shared partial which yields some content, the content gets inserted at the wrong location.

In this test it should be inside the label tag – <label>world</label> – but it is rendered outside: world <label></label>.

For now this MR just adds a failing test for you to reproduce which is the way bug reports are expected according to the issue template.

Is there somebody that can take a look at this? Thank you very much.

When passing the from helper to a shared partial which yields some
content, the content gets inserted at the wrong location.

In this test it should be inside the label tag – `<label>world</label>`
– but it is rendered outside: `world <label></label>`.
@joelhawksley
Copy link
Copy Markdown
Member

Thank you for the failing test PR! I'll continue this work in #2619.

zangab pushed a commit to platoyo/view_component that referenced this pull request May 18, 2026
When a component renders a partial that uses a Rails form helper (like
form.label) with a yielding block, the yielded content was rendered
outside the helper's tag instead of inside it.

Root cause: form_with creates form builders with @template_object
pointing to the component. When form.label calls capture through the
component during partial rendering, the component's @output_buffer
(set once in render_in) is stale — Rails' _run has already replaced
the view context's output buffer with a fresh one for the partial.
The capture reads from the wrong buffer while the block writes to the
partial's buffer, causing the content to leak.

Fix: Override capture in ViewComponent::Base to sync @output_buffer
with view_context.output_buffer before delegating to super.

Fixes ViewComponent#2617

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

2 participants