Skip to content

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

Closed
@mdrbohlav

Description

@mdrbohlav

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions