Skip to content

render_in returns non-html_safe empty string when render? is false #2583

@owaiswiz

Description

@owaiswiz

When a component's render? returns false, render_in returns a plain "" instead of "".html_safe.
This is inconsistent with the happy path, which always returns an ActiveSupport::SafeBuffer.

https://github.com/ViewComponent/view_component/blob/main/lib/view_component/base.rb#L163

This is problematic when concatenating a component with something

e.g

   render(MyComponent.new) + some_other_method_that_returns_html

I know i can html_safe explicitly the output of the render call, but would prefer if I didn't have to worry about it.

Steps to reproduce

  class MyComponent < ViewComponent::Base
    def render? = false
  end

  result = render(MyComponent.new)

Expected behavior

  result.html_safe?  # => true
  result.class            # => ActiveSupport::SafeBuffer (or String is fine too as long as it's html_safe)

Actual behavior

  result.html_safe?  # => false
  result.class            # => String

System configuration

Rails version: 8.1.2

Ruby version: 3.3

Gem version: 4.4.0

Metadata

Metadata

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