view_component: render_many doesn't work right with Slim

Steps to reproduce

# app/components/foo_component.rb
class FooComponent < ViewComponent::Base
  include ViewComponent::SlotableV2

  renders_many :bars, BarComponent
end
# app/components/foo_component.html.slim
- bars.each do |bar|
    = bar
# app/components/foo_component.html.slim
class BarComponent < ViewComponent::Base
end
# app/components/foo_component.html.slim
= content
# app/views/whatever.html.slim
= render FooComponent.new do |c|
  - c.bar do
    p Something with HTML formatting

Expected behavior

When the HTML is rendered, the text “Something with HTML formatting” should be formatted correctly.

Actual behavior

The HTML is escaped and rendered in the page so the end-user sees the HTML tags instead of the formatting.

System configuration

Rails version: 6.0.3.4

Ruby version: 2.7.2p137

Gem version: 2.24.0

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 15 (15 by maintainers)

Most upvoted comments

I was kind of imagining something like this, which would be almost like delegating html_safe? to the underlying template.

class SlotV2
  def html_safe?
    @html_safe if defined?(@html_safe)
  end

  def to_s
    # some existing code
    content = capture do
      # more existing code
    end

    @html_safe = content.html_safe
    content
  end
end

Could something like that work?

@joelhawksley Yeah, I can try and do that tomorrow