Replies: 2 comments 4 replies
-
This might be a typo, have you tried this instead? render(ExampleComponent.new(title: "My Title") { "Hello, World!" }) |
Beta Was this translation helpful? Give feedback.
4 replies
-
I ran into this issue too, in your views, you should use content rather than yield. content is the method provided to output the block given to the component. https://viewcomponent.org/api.html#content--string class Bootstrap::AccordionComponent::ItemComponent < BootstrapComponent
def initialize(parent: nil, header: "Default header", open: false, kontent: nil)
@id = SecureRandom.hex(6).parameterize
@open = !!open
@parent = parent
@header = header
@content = kontent
end
def header_classes
[initial_header_state]
end
def body_classes
[initial_body_state]
end
def bs_parent
"##{@parent}" if @parent
end
private
def initial_header_state
@open ? "" : "collapsed"
end
def initial_body_state
@open ? "show" : ""
end
end .accordion-item
.accordion-header
%b.accordion-button{ type: "button",
class: header_classes,
data: { bs_toggle: "collapse",
bs_target: "##{@id}",
bs_parent: "##{@parent}" },
aria: { expanded: @open ? "true" : "false",
controls: @id } }
= @header
.accordion-collapse.collapse{ id: @id, class: body_classes, data: {bs_parent: bs_parent} }
.accordion-body
= @content
= content |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The "Getting started" page in the guide says that this should work:
but my experience is that the block is not passed through to the component and so
content
isnil
in the component's template. I don't think this is due to an issue in ViewComponent either: from my reading of the code (rails/actionpack/lib/abstract_controller/rendering.rb
), therender
method in a Rails controller does not seem to expect a block. Therender
method includes&block
in the signature but it seems to go nowhere.This alternative should work:
but it gets a bit untidy with nested components, particularly as
with_content
doesn't seem to handle a ViewComponent being passed to it directly. You also can't userender
more than once in a controller action, so we have:I noticed that ViewComponent provided
render_component
for Rails < 6.1, but that also didn't take a block. But I wonder if something like this would be possible:Is it worth fixing Rails'
render
to pass the block through and clean this up further?Beta Was this translation helpful? Give feedback.
All reactions