render with block in controller #1188
-
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 This alternative should work:
but it gets a bit untidy with nested components, particularly as
I noticed that ViewComponent provided
Is it worth fixing Rails' |
Beta Was this translation helpful? Give feedback.
Replies: 3 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.
-
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.
-
The Getting Started guide has long since been updated to reflect that passing blocks to components does not work in controllers: https://viewcomponent.org/guide/getting-started.html#rendering-from-controllers. |
Beta Was this translation helpful? Give feedback.
The Getting Started guide has long since been updated to reflect that passing blocks to components does not work in controllers: https://viewcomponent.org/guide/getting-started.html#rendering-from-controllers.