ActionView::Helpers::FormHelper
def form_for(record, options = {}, &block)

Description

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

Arguments

Changing a value updates the ERB and HTML.

Accepts:
  • An object: like model: in form_with, determines the prefix of the name and id attributes of the fields in the block, and the defaults of the :url and :method options. Unlike form_with, adds class and id to the <form>. Both are built from the model name and whether the object is persisted. If it is persisted, the object's id is also appended to the id attribute.
  • A symbol or string (such as :article): like scope: in form_with, determines the prefix of the name and id attributes. If there is an instance variable with the same name as the symbol, its value provides the initial values of the fields. In this case, :as is ignored, and the :id and :class options become attributes of the <form> as is.
  • An array: the same as an array given to model: in form_with. The names, class and id are built from the last element.
  • nil: raises ArgumentError.
options= {}
"post"
Replaces the model name as the prefix of the name and id attributes of the fields in the block. The class and id of the <form> are also built from this value. In this case, the object's id is not appended to the id attribute of the <form>, even if the object is persisted.
"admin"
Prepended to the id attribute of the fields in the block and of the <form>. form_with does not prepend it to the id attribute of the <form>.
true adds data-remote="true". Same as local: false in form_with, except that the hidden <input> for the CSRF token is not added. It is added if authenticity_token is also given, or in apps where config.action_view.embed_authenticity_token_in_remote_forms is true.
"article_form"
Ignored when record is an object. To set the id of the <form>, pass it in :html.
"article_form"
Ignored when record is an object. To set the class of the <form>, pass it in :html.
{id: "article_form", class: "article_form"}
Adds the entries of the hash as HTML attributes of the <form>. As with the html of form_with, method, multipart, remote, authenticity_token and enforce_utf8 do not become HTML attributes, and action is ignored. id and class given here replace the values built from record.
Ignored. Unlike form_with, id is always added to the fields in the block.
true creates fields with no initial value for attributes the model does not have. By default, Rails tries to read such an attribute from the model and raises NoMethodError, the opposite of form_with's default (try f.text_field :subtitle in &block).
form_for rewrites the options and then calls form_with. Options not listed here (url, format, method, multipart, authenticity_token, index, builder, data, etc.) are the same as in form_with. However, :local, :scope and :model are ignored (they are determined by remote in html, :as and record).
&block= nil
Required, unlike in form_with. Omitting it raises ArgumentError.

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.