palkan / view_component-contrib

A collection of extension and developer tools for ViewComponent
MIT License
369 stars 22 forks source link

The content returns nil in the ViewComponent #5

Closed dpaluy closed 3 years ago

dpaluy commented 3 years ago

I want to render a block content render_component Button::Component.new { "Welcome!" }

# button/component.html.erb

<div class="bg-primary text-white">
   <%= content %> 
</div>

But the content is nil. The original ViewComponent can render a content properly.

Is it a bug, or there is a different way to render the content?

Environment

Ruby Version: ruby 3.0.2p107 Rails: 7.0.0.alpha2 View Component version: 2.40.0 View Component Contrib Version: 0.1.0

thomasklemm commented 3 years ago

@dpaluy Could you try render_component(Button::Component.new() { "Welcome!" })? I think I had an issue a while back related to ViewComponent where the block wasn't properly passed in as child content unless doing Component.new() { "child content" }. Could be mistaken though and it's unrelated.

palkan commented 3 years ago

What's your version of view_component gem?

And try the @thomasklemm's suggestion.

dpaluy commented 3 years ago

Following @thomasklemm guide, I debug this issue and found that render_component doesn't have a block attribute.

I solved this issue by using a render instead https://github.com/github/view_component/blob/main/lib/view_component/preview.rb#L10

Example:

render(Button::Component.new) do
  "Welcome"
end

@palkan my View Component version: 2.40.0