Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

&block rendered twice or at the wrong position from helper #1384

Closed
mdrbohlav opened this issue Jun 8, 2022 · 3 comments · Fixed by #1650
Closed

&block rendered twice or at the wrong position from helper #1384

mdrbohlav opened this issue Jun 8, 2022 · 3 comments · Fixed by #1650

Comments

@mdrbohlav
Copy link

I have a custom form builder but since I want to be able to use the default one as well I created helper method for it:

# form_helper.rb

module FormHelper
  def my_form_with(**args, &block)
    form_with(builder: MyFormBuilder, **args, &block)
  end
end

And when I try to use it inside any component I get two different results but neither of them is what I expect it to be. One renders the block twice - before and inside the form tag. The other one renders the block only before the form tag.

Note that when I use the form_with directly in the component's template it works so this has something to do with it being called from the helper.

Steps to reproduce

Create helper that receives &block, pass that block to any rails helper like form_with, link_to, etc. Use the created helper in the component.

Expected behavior

It should render the block inside the form tag and only once. The same way it works when used outside the component

Actual behavior

Example 1:

# component.html.erb

<%= helpers.my_form_with do %>
  Example string
<% end>

Result:

Example string

<form action="..." method="post">
    Example string
</form>

Example 2:

# component.html.erb

<%= helpers.my_form_with(model: Model) do |form| %>
  <% form.text_field(:title) %>
<% end>

Result:

<input id="model_title" type="text" name="model[title]">

<form action="..." method="post">
</form>

System configuration

Rails version: 6.1

Ruby version: 2.7.6

Gem version: 2.56.2

@mdrbohlav mdrbohlav changed the title &block rendered twice from helper &block rendered twice or at the wrong position from helper Jun 8, 2022
@Spone
Copy link
Collaborator

Spone commented Jun 8, 2022

See #974 for the WIP on this issue 🙂

@mdrbohlav
Copy link
Author

I have found some issues with the similar problem but totally missed the PR, thank you!

@hulloitskai
Copy link

I believe that another PR aimed to resolve this issue was actually merged (#1307), and then subsequently reverted: #1432.

I've recently run into this issue, and I was wondering if there are any efforts being made to address it, now that we are no longer experimenting with a single output buffer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants