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
| Prop | Type | Default | Description |
|---|---|---|---|
variant | Symbol:dropzone | :button | :dropzone | Large drag & drop area or a compact button. Files can be dropped on the field in both cases. |
size | Symbol:sm | :md | :lg | :md | Size of the field. |
multiple | Boolean | false | Accept several files. In single mode a new pick replaces the previous file. |
accept | String | Array | nil | Accepted types, in `accept` syntax (e.g. `"image/*,.pdf"` or `["image/*", ".pdf"]`). Filters the file dialog and rejects dropped files. |
max_file_size | Integer | nil | Maximum size of a single file, in bytes (e.g. `8.megabytes`). |
max_files | Integer | nil | Maximum number of files. Ignored unless `multiple: true`. |
prompt | String | "Click to upload", or "Click to upload files" when multiple | Main line of text inside the drop area. Only used by the `:dropzone` variant. |
caption | String | false | generated 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_label | String | "Choose file", or "Choose files" when multiple | Label of the trigger button. Only used by the `:button` variant. |
s3_endpoint | String | nil | Upload 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. |
label | String | false | humanized attribute name | Custom label text. Set to `false` to hide the label. |
hint | String | nil | Helper text displayed below the field. |
error | String | nil | Manual error message. ActiveRecord errors are displayed automatically. |
disabled | Boolean | false | Disable 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
<%= 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
<%= form_with url: root_path do |f| %>
<%= f.uploader :locked_field, disabled: true, hint: "Attachments are closed for this record" %>
<% end %>Validation
<% 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 %>