instance method file_field

Ruby on Rails 8.0.4

Since v3.1.12

Available in: v3.1.12 v3.2.22.5 v4.0.13 v4.1.16 v4.2.9 v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

file_field(method, options = {})

Returns a file upload input tag tailored for accessing a specified attribute (identified by method) on an object assigned to the template (identified by object). Additional options on the input tag can be passed as a hash with options. These options will be tagged onto the HTML as an HTML element attribute as in the example shown.

Using this method inside a #form_with block will set the enclosing form’s encoding to multipart/form-data.

Options
  • Creates standard HTML attributes for the tag.

  • :disabled - If set to true, the user will not be able to use this input.

  • :multiple - If set to true, *in most updated browsers* the user will be allowed to select multiple files.

  • :include_hidden - When multiple: true and include_hidden: true, the field will be prefixed with an <input type="hidden"> field with an empty value to support submitting an empty collection of files. Since include_hidden will default to config.active_storage.multiple_file_field_include_hidden if you don’t specify include_hidden, you will need to pass include_hidden: false to prevent submitting an empty collection of files when passing multiple: true.

  • :accept - If set to one or multiple mime-types, the user will be suggested a filter when choosing a file. You still need to set up model validations.

Examples
# Let's say that @user has avatar:
file_field(:avatar)
# => <input type="file" id="user_avatar" name="user[avatar]" />

# Let's say that @article has image:
file_field(:image, :multiple => true)
# => <input type="file" id="article_image" name="article[image][]" multiple="multiple" />

# Let's say that @article has attached:
file_field(:attached, accept: 'text/html')
# => <input accept="text/html" type="file" id="article_attached" name="article[attached]" />

# Let's say that @article has image:
file_field(:image, accept: 'image/png,image/gif,image/jpeg')
# => <input type="file" id="article_image" name="article[image]" accept="image/png,image/gif,image/jpeg" />

# Let's say that @attachment has file:
file_field(:file, class: 'file_input')
# => <input type="file" id="attachment_file" name="attachment[file]" class="file_input" />

Parameters

method req
options opt = {}
Source
# File actionview/lib/action_view/helpers/form_helper.rb, line 2558
      def file_field(method, options = {})
        self.multipart = true
        @template.file_field(@object_name, method, objectify_options(options))
      end

Defined in actionview/lib/action_view/helpers/form_helper.rb line 2558 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActionView::Helpers::FormBuilder

Type at least 2 characters to search.

Use the arrow keys to navigate results, Enter to open one, Escape to close.

Keyboard shortcuts

/
Focus search
⌘K / Ctrl-K
Command palette
?
This help
Esc
Close