Return values from component with yield
nicolas-brousse opened this issue · comments
I'm trying to create a component to have a form container. And so use yield to pass the form helper to the view from the component.
So I did the following.
# app/views/post/_form.html.slim
= c "form", model: @post, local: true do |f|
div
= f.label :name
= f.text_field :name
# frontend/components/form/_form.html.slim
= form_with(model: @model, local: @local) do |form|
.form(data-controller="form")
= yield(form)
But I got this error : ActionView::Template::Error (undefined method
label' for nil:NilClass)`.
I tried to print the content of form
or f
. I have the form helper object inside the container, but in the view f
is nil.
Does the block inside the view is rendered before the component?
Hello @nicolas-brousse,
You can't do that now, but we can introduce this behavior.
You can override the component helper in your own project by defining a custom component helper in your app/helpers/pplication_helper.rb
:
def component(component_name, locals = {}, options = {}, &block)
captured_block = proc { |args| capture(args, &block) } if block_given?
Komponent::ComponentRenderer.new(
controller,
).render(
component_name,
locals,
options,
&captured_block
)
end
alias :c :component
Thanks @florentferry.
It works for me.
It could be nice to have it implemented inside Komponent 😃