Skip to content

helpers.link_to with a block causes broken HTML output #1485

Closed
@ZilvinasKucinskas

Description

@ZilvinasKucinskas

helpers.link_to does not work with a block. Removing helpers prefix fixes the behavior.

However, it is very common to use link_to in view helpers. A simple usage is marking the links as active or inactive based on the current URL:

module LinksHelper
  def link_to_active(name = nil, options = {}, html_options = {}, &block)
    # For simplicity logic omitted

    link_to(name, options, html_options, &block)
  end
end

Usage of link_to_active with a block just breaks view_component.

Steps to reproduce

Inside a component, render:

<%= helpers.link_to "/", class: "flex" do %>
  <%= helpers.heroicon :check %>
  <span class="ml-2">Link</span>
<% end %>

Expected behavior

<a class="flex" href="/">
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" class="h-5 w-5">
    <path fill-rule="evenodd" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" clip-rule="evenodd"></path>
  </svg>
  <span class="ml-2">Link</span>
</a>

Actual behavior

It renders incorrect HTML:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" class="h-5 w-5">
  <path fill-rule="evenodd" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" clip-rule="evenodd"></path>
</svg>
<span class="ml-2">Link</span>
<a class="flex" href="/">
  &lt;span class="ml-2"&gt;Link&lt;/span&gt;
</a>

System configuration

Rails version: Rails 7.0.3.1
Ruby version: ruby 3.1.2p20
Gem version: 2.66.0

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