I have a helper that I am using to generate a form. Parameters that are used to generate the form's fields are passed into the helper. I can't figure out how to use the block outside of a template.
For example:
def generate_form(path, fields)
form_tag(path, method: :get) do
# what do I do in here?
end
end
When I render partials within the block, nothing appears in the rendered web page. If I join together a bunch of tags (field_tag, text_field_tag, etc.), then raw html appears on the page.
I am using Rails 3.1.0
form_tag generates an HTML form for us and lets you specify options you want for your form. In the example below, we use the rails method, url_for, to create a URL we want the form to submit to.
Rails element helpers return strings, so you can do:
def generate_form(path, fields)
s = form_tag(path, method: :get) do
p = input_tag
p << submit_tag #(everything will be wrapped in form tag)
p #returns p from block
end
s.html_safe #returns s and avoids html escaping
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With