How to pass arguments back to slot content blocks? #1696
Answered
by
andrewhavens
andrewhavens
asked this question in
Q&A
-
This is a contrived example, but it demonstrates what I'm trying to do. I've tried to figure out how to solve this and can't figure out a way. <%= render MyFormWrapperComponent.new do |c| %>
<% c.with_form_fields do |form| %>
<%= form.text_field :name %>
<% end %>
<% end %> class MyFormWrapperComponent.new < ViewComponent::Base
renders :form_fields
end <%= form_with(...) do |form| %>
<% if form_fields? %>
<%= form_fields.call(form) %>
<% end %>
<% end %> |
Beta Was this translation helpful? Give feedback.
Answered by
andrewhavens
Mar 29, 2023
Replies: 1 comment
-
Ahhh! I figured out how to do it. The trick is to use a lambda slot and return the result of calling the block. class MyFormWrapperComponent.new < ViewComponent::Base
renders :form_fields, -> (&block) do
form_fields_content = nil
form_with(...) do |form|
form_fields_content = block.call(form)
end
form_fields_content
end
end <%= form_with(...) do |form| %>
<% if form_fields? %>
<%= form_fields %>
<% end %>
<% end %> |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
andrewhavens
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ahhh! I figured out how to do it. The trick is to use a lambda slot and return the result of calling the block.