ActionView::Helpers::FormHelper
def form_with(model: false, scope: nil, url: nil, format: nil, **options, &block)

Description

Creates a <form> and outputs the content of the block inside it.

Arguments

Changing a value updates the ERB and HTML.

= false
Uses the given object's values as the initial values of the fields in the block. The defaults of the scope and url arguments and the :method option also come from this object.
  • An array ([@article, @comment]): url is built from the whole array (/articles/1/comments/1), and everything else comes from the last object in the array.
  • nil: raises ArgumentError.
= nil
The prefix of the name and id attributes of the fields in the block. Defaults to the model name if there is a model; otherwise there is no prefix, and the name is just the attribute name. If there is no model and there is an instance variable with the same name as scope, its value provides the initial values of the fields.
= nil
Used as the action attribute of the <form>. Defaults to a URL built from model if there is one, and otherwise to the URL of the request that rendered the form. On this site the ERB is rendered by a request to /api/render, so the default is /api/render. false omits action.
= nil
Appended to the URL built from model. Ignored without model, or when url is given.
options= {}
Depending on the value:
  • :get: 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 reads _method from the incoming POST and handles the request as a PATCH, PUT or DELETE.
  • Omitted: :patch for a persisted model, and :post otherwise.
false adds data-remote="true". In apps without Rails UJS (the default since Rails 7), this HTML attribute does nothing.
true adds enctype="multipart/form-data". It is also added when the block uses f.file_field, even if this is omitted or false.
false omits the hidden <input> for the CSRF token.
"article_form"
Used as the id attribute of the <form>.
{novalidate: true}
Adds the entries of the hash as HTML attributes of the <form>. However, method, multipart, remote, authenticity_token and enforce_utf8 are handled as if given in options and do not become HTML attributes. action is ignored (false omits action). id, class and data given here take precedence over the same keys in options.
"admin"
Prepended to the id attribute of the fields in the block. The name attribute does not change.
1
Inserted as an index into the name and id attributes of the fields in the block. Not added if there is neither model nor scope. nil adds [] instead of an index (article[][title]).
true omits id from the fields in the block and for from f.label. By default, id is added, because config.action_view.form_with_generates_ids is true (with load_defaults 5.2 or later).
false makes Rails try to read attributes the model does not have from the model, which raises NoMethodError. By default, fields for such attributes are created with no initial value (try f.text_field :subtitle in &block).
LabeledFormBuilder
Changes the class of the block's f (a FormBuilder). LabeledFormBuilder on this page overrides FormBuilder's text_field to add a label for the same attribute before the text_field.
Common options

Rails handles these the same way in every helper. However, form_with turns only class and data into HTML attributes of the <form>.

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.
Options not listed here do not become HTML attributes of the <form>; they are passed to the block's form builder as settings. Pass any other HTML attributes for the <form> in :html. accept-charset="UTF-8" is always added; even with a different value in :html, it stays UTF-8.
&block= nil

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 hides the CSRF token as (hidden). The real token is a long string that changes on every render.