ActionView::Helpers::FormBuilder
def fields_for(record_name, record_object = nil, fields_options = nil, &block)

Description

Creates fields bound to the attributes of an associated model inside the outer form.

Arguments

Changing a value updates the ERB and HTML.

Inserted into the middle of the name and id of the elements created in the block. How it is inserted depends on whether the model has a method such as comments_attributes= (created by accepts_nested_attributes_for):
  • Yes: treated as a nested association, and _attributes is appended to the name in name and id.
    • An association with many records (has_many): adds an index and repeats the block once for each record (outputs nothing if there are none).
    • An association with one record (has_one): adds no index and outputs the block's content once. Outputs nothing if there is no associated record.
  • No: inserts the name as is, as in article[author][name].
= nil
Accepts:
  • Omitted: for a nested association, the associated records (@article.comments) are used. Otherwise, no object is used, so the elements have no initial value.
  • A single object (@comment): even when record_name is a has_many association (:comments), only that object is used instead of @article.comments, and the block's content is output once.
  • An array: used instead of the associated records, and the block is repeated for each element.
fields_options= nil
false omits the hidden <input> that holds the id of a persisted record. By default, inherits the value given to the outer form, and if the outer form has none, adds the hidden <input> after the block. It is not added when the block creates its own with c.hidden_field :id.
5
Uses the given value as the index of the nested association instead of counting from 0, so every record gets the same index. A proc is called for each record, and its return value is used.
3
Adds this value as an index to the name and id of the elements created in the block. In a nested association, it comes after the record's index. By default, a nested association gets no index, and a name that is not an association (such as :author) inherits the :index given to the outer form. nil adds [] instead of an index (article[author][][name]).
true omits id from the elements created in the block, and for from c.label. By default, id is added. Not inherited from the outer form.
true creates elements without an initial value for attributes the model does not have. By default, tries to read such an attribute from the model and raises NoMethodError. Not inherited from the outer form (try c.text_field :author_name in &block).
Options not listed here are handled as follows:
  • namespace: ignored; the outer form's value is used.
  • builder: defaults to the outer form's builder class.
  • Others (such as class): do not become HTML attributes; they are passed as options to the block's form builder.
&block= nil
Required. Raises LocalJumpError if omitted.

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.