instance method file_field

Ruby on Rails 4.0.13

Since v2.2.3

Available in: v2.2.3 v2.3.18 v3.0.20 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(object_name, 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_for 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.

  • :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

file_field(:user, :avatar)
# => <input type="file" id="user_avatar" name="user[avatar]" />

file_field(:post, :image, :multiple => true)
# => <input type="file" id="post_image" name="post[image]" multiple="true" />

file_field(:post, :attached, accept: 'text/html')
# => <input accept="text/html" type="file" id="post_attached" name="post[attached]" />

file_field(:post, :image, accept: 'image/png,image/gif,image/jpeg')
# => <input type="file" id="post_image" name="post[image]" accept="image/png,image/gif,image/jpeg" />

file_field(:attachment, :file, class: 'file_input')
# => <input type="file" id="attachment_file" name="attachment[file]" class="file_input" />

Parameters

object_name req
method req
options opt = {}
Source
# File actionpack/lib/action_view/helpers/form_helper.rb, line 841
      def file_field(object_name, method, options = {})
        Tags::FileField.new(object_name, method, self, options).render
      end

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

Defined in ActionView::Helpers::FormHelper

Type at least 2 characters to search.

↑↓ navigate · open · esc close