ActionView::Helpers::FormTagHelper
def form_tag(url_for_options = {}, options = {}, &block)

Description

Creates a <form> that is not tied to a model.

Arguments

Changing a value updates the ERB and HTML.

Sets the action of the <form>. Depending on the value:
  • A string: used as the action as is.
  • A hash: builds the URL from the routes, as url_for does ({ controller: "articles", action: "index" } gives /articles). Keys you leave out are filled in from the current request, so an empty hash ({}) gives the URL of the request that rendered the form. On this site, that is /api/render.
  • false: omits action.
The :action option does not override this argument, except that action: false omits action whatever the argument is. Other values (nil, :back, a model object, etc.) are passed to url_for (nil is the same as {}).
options= {}
Depending on the value:
  • :get: sets method="get" and omits the hidden <input> for the CSRF token (other values add it).
  • :patch, :put or :delete: sets method="post" and also adds a hidden <input> for _method. Rails treats an incoming POST with _method as a request of that method, such as PATCH.
  • Omitted: sets method="post".
true adds enctype="multipart/form-data". Unlike form_with with f.file_field, it is not added unless this option is given, even if the block contains a file <input>. Overrides an enctype given with it, with multipart/form-data.
true adds data-remote="true". In apps without Rails UJS (the default since Rails 7), this HTML attribute does nothing. In apps where config.action_view.embed_authenticity_token_in_remote_forms is false, the hidden <input> for the CSRF token is omitted too (this site leaves it at the default, nil, so it is added).
false omits the hidden <input> for the CSRF token. A string is used as the value of the hidden token input. true adds the token even with remote: true in such apps.
true adds a hidden <input> with name="utf8" and a value of ✓. Defaults to config.action_view.default_enforce_utf8 (false since load_defaults 6.0).
Common options

Rails handles these options the same way in every helper. Options the HTML spec does not allow on the <form>, and ones this site considers rarely used, are not listed here.

Shared by all helpers

{active: true, hidden: false}
A hash gives the keys whose values are not false or nil, joined with spaces. An array gives its elements joined with spaces, after removing nil, false and empty strings (including whitespace-only strings).
{controller: "tags", tags_suggestions_value: ["ruby", "rails"]}
A hash adds data-* attributes, with _ in the keys converted to -. Array and hash values are converted to JSON strings.
{describedby: ["name_help", "name_error"], invalid: false}
Like :data, adds aria-* attributes. Array values are joined with spaces, leaving out nil and empty strings (nothing is added if none remain). For a hash value, the keys whose values are not false or nil are joined with spaces.
Any other options become HTML attributes of the <form>; those with a nil value are left out. accept-charset="UTF-8" is always added: even if another value is passed for accept-charset, it stays UTF-8.
&block= nil
With a block, uses its output as the content of the <form> and closes it with </form>. Without one, returns only the opening tag and the hidden <input> elements, without </form> (write the closing tag yourself).

ERB

Changes from the previous output: Added Removed Changed


          

HTML


          

Preview

Interacting with the preview updates the form submission.

Form submission

Form data

namevalue
Request body

Content-Type:


              

params


              
This site shows the CSRF token as (hidden). The real token is a long string that changes on every render.