ActionView::Helpers::FormHelper
def file_field(object_name, method, options = {})

Description

Creates an <input> with type="file" for a model attribute.

Arguments

Changing a value updates the ERB and HTML.

object_name:user
method:avatar
options= {}
Used only with multiple: true. true adds a hidden <input> before the file <input>, with the same name and an empty value. By default, the hidden <input> is added, because config.active_storage.multiple_file_field_include_hidden is true (with load_defaults 7.0 or later).
["image/png", "image/jpeg"]
Adds accept with the given value. An array is joined with commas.
true adds data-direct-upload-url, set to the URL for Active Storage direct uploads. Without the Active Storage routes, adds nothing. On this site the URL is a dummy in the same format as the real one and does not accept uploads.
Common options

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

Shared by form helpers

true adds the multiple attribute and appends [] to name.
"q"
Replaces the generated name with the given value. nil omits name.
nil
Replaces the generated id with the given value. nil omits id.

Shared by all helpers

true adds disabled="disabled"; false omits it. The same goes for every HTML attribute in Rails' list (BOOLEAN_ATTRIBUTES), such as readonly, required and autofocus: any value other than false or nil adds it as attr="attr".
{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 <input type="file">; those with a nil value are left out.

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


              
On this site, only the first 128 bytes of the file content are sent. The request body shows the file content as (binary), and the params section hides the path of the temporary file on the server as (hidden).