Uploader

File field with drag & drop, client-side restrictions and image previews.

    Drop a file or pick one from your computer

    Usage

    <%= form_with url: root_path do |f| %>
      <%= f.uploader :avatar, accept: "image/*", max_file_size: 8.megabytes %>
      <%= f.uploader :documents, multiple: true, max_files: 5 %>
      <%= f.uploader :report, variant: :button %>
      <%= f.uploader :attachments, multiple: true, s3_endpoint: "/uploads" %>
    <% end %>
    Props
    PropTypeDefaultDescription
    variantSymbol
    :dropzone | :button
    :dropzoneLarge drag & drop area or a compact button. Files can be dropped on the field in both cases.
    sizeSymbol
    :sm | :md | :lg
    :mdSize of the field.
    multipleBooleanfalseAccept several files. In single mode a new pick replaces the previous file.
    acceptString | ArraynilAccepted types, in `accept` syntax (e.g. `"image/*,.pdf"` or `["image/*", ".pdf"]`). Filters the file dialog and rejects dropped files.
    max_file_sizeIntegernilMaximum size of a single file, in bytes (e.g. `8.megabytes`).
    max_filesIntegernilMaximum number of files. Ignored unless `multiple: true`.
    promptString"Click to upload", or "Click to upload files" when multipleMain line of text inside the drop area. Only used by the `:dropzone` variant.
    captionString | falsegenerated from `max_file_size` and `max_files`Secondary line of text, e.g. `Up to 8 MB · max 5 files`. Pass your own text to replace it, or `false` to hide it.
    button_labelString"Choose file", or "Choose files" when multipleLabel of the trigger button. Only used by the `:button` variant.
    s3_endpointStringnilUpload files straight to S3 as soon as they are added. The form then submits the S3 object key of each file in a hidden field instead of the file, and waits for pending uploads before it submits. Emits `uploader:uploaded` and `uploader:failed`. Your app provides a Companion-compatible signing endpoint: `GET {endpoint}/s3/params` must return a presigned POST (`{ url, fields: { key, ... } }`), not a presigned PUT; files over 100 MB use multipart, so `{endpoint}/s3/multipart` routes are needed as well. These requests carry no CSRF token, so a Rails controller serving them must skip forgery protection. The bucket CORS must allow `POST` (and `PUT` for multipart) from your origin and expose the `ETag` header. Submitted keys come from the browser: check them on the server (expected prefix, object exists) before attaching.
    labelString | falsehumanized attribute nameCustom label text. Set to `false` to hide the label.
    hintStringnilHelper text displayed below the field.
    errorStringnilManual error message. ActiveRecord errors are displayed automatically.
    disabledBooleanfalseDisable the field. Picking and dropping files are both turned off.

    Also accepts any HTML attributes via **options (e.g., id:, data:, aria:). class: is also supported for custom styling.

    Examples

    Basic Uploader

      <%= form_with url: root_path do |f| %>
        <%= f.uploader :attachment %>
      <% end %>

      Multiple Files

        <%= form_with url: root_path do |f| %>
          <%= f.uploader :documents, multiple: true, max_files: 3, max_file_size: 2.megabytes, accept: ".pdf,.doc,.docx" %>
        <% end %>

        Images Only

          Square JPG or PNG works best

          <%= form_with url: root_path do |f| %>
            <%= f.uploader :cover, accept: "image/*", max_file_size: 8.megabytes, hint: "Square JPG or PNG works best" %>
          <% end %>

          Button Variant

              <%= form_with url: root_path do |f| %>
                <%= f.uploader :report, variant: :button %>
                <%= f.uploader :screenshots, variant: :button, multiple: true, button_label: "Attach screenshots", accept: "image/*" %>
              <% end %>

              Uploader Sizes

                    <%= form_with url: root_path do |f| %>
                      <%= f.uploader :large_field, size: :lg %>
                      <%= f.uploader :default_field %>
                      <%= f.uploader :small_field, size: :sm %>
                    <% end %>

                    Custom Texts

                          <%= form_with url: root_path do |f| %>
                            <%= f.uploader :invoice, label: "Invoice", prompt: "Upload the signed invoice", caption: "Scan or photo, PDF preferred" %>
                            <%= f.uploader :receipt, max_file_size: 4.megabytes, caption: false %>
                            <%= f.uploader :no_label, label: false %>
                          <% end %>

                          Disabled State

                            Attachments are closed for this record

                            <%= form_with url: root_path do |f| %>
                              <%= f.uploader :locked_field, disabled: true, hint: "Attachments are closed for this record" %>
                            <% end %>

                            Validation

                              Is required

                                This field has a custom error

                                <% user = User.new %>
                                <% user.errors.add(:avatar, "is required") %>
                                <%= form_with model: user, url: root_path do |f| %>
                                  <%= f.uploader :avatar, accept: "image/*" %>
                                  <%= f.uploader :avatar, error: "This field has a custom error" %>
                                <% end %>

                                Direct Upload to S3

                                <%# Needs a signing endpoint in your app, see the s3_endpoint prop above. %>
                                <%# The form submits attachments[] with the S3 key of each file instead of the files. %>
                                <%= form_with model: @message do |f| %>
                                  <%= f.uploader :attachments, multiple: true, s3_endpoint: "/uploads", max_file_size: 50.megabytes %>
                                <% end %>

                                Are you absolutely sure?

                                This cannot be undone.