Skip to content

Form inputs inside a slot #1126

Answered by boardfish
mbajur asked this question in Q&A
Nov 8, 2021 · 2 comments · 6 replies
Discussion options

You must be logged in to vote

There's probably a cleaner way to do this, but what I've got so far is:

  1. Define a component with a lambda slot as follows: renders_one :form, ->(&block) { form_with(**form_opts, &block) }. form_opts can be a method on the component that returns the hash of options you'd otherwise pass to form_with, fields, or fields_for. You can also change the lambda to accept params if you wish.
  2. Render it like this:
<%= render FormComponent.new do |c| %>
  <% c.form do |f| %>
    <%= f.text_field :query %>
  <% end %>
<% end %>

If you're using fields or fields_for rather than form_with, you could probably delegate_missing_to those because they don't need to render a surrounding form tag, but I haven't …

Replies: 2 comments 6 replies

Comment options

You must be logged in to vote
5 replies
@boardfish
Comment options

@mbajur
Comment options

@boardfish
Comment options

@mbajur
Comment options

@boardfish
Comment options

Answer selected by mbajur
Comment options

You must be logged in to vote
1 reply
@vokshirg
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants