JavaScript library for direct file uploads to cloud storage services in Rails applications
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Automatic integration with Rails forms providing seamless direct file upload functionality through DOM event handling.
Initializes Active Storage automatic direct upload handling by setting up DOM event listeners for forms containing direct upload file inputs.
/**
* Initializes Active Storage automatic direct upload handling
* Sets up DOM event listeners for forms with direct upload file inputs
* Can be called multiple times safely - will only initialize once
*/
function start(): void;Usage Examples:
import { start } from "@rails/activestorage";
// Initialize after DOM content loaded
document.addEventListener("DOMContentLoaded", () => {
start();
});
// Or initialize immediately if DOM is already ready
start();The start() function automatically handles:
When start() is called, the library automatically:
data-direct-upload-url attributedata-direct-uploads-processing attributeRequired HTML Structure:
<form action="/posts" method="post" enctype="multipart/form-data">
<!-- File input must have data-direct-upload-url attribute -->
<input type="file"
name="post[attachments][]"
data-direct-upload-url="/rails/active_storage/direct_uploads"
multiple>
<button type="submit">Create Post</button>
</form>The automatic integration dispatches custom DOM events throughout the upload process:
Form-level events (dispatched on <form> element):
direct-uploads:start - When upload process beginsdirect-uploads:end - When all uploads complete successfullyInput-level events (dispatched on <input> element):
direct-upload:initialize - For each file when upload is createddirect-upload:start - When individual file upload beginsdirect-upload:before-blob-request - Before blob metadata requestdirect-upload:before-storage-request - Before file storage uploaddirect-upload:progress - During file upload progressdirect-upload:error - When upload error occursdirect-upload:end - When individual file upload completesEvent Listening Examples:
// Listen for upload progress
document.addEventListener("direct-upload:progress", (event) => {
const { progress, file } = event.detail;
console.log(`${file.name}: ${Math.round(progress)}%`);
});
// Handle upload errors
document.addEventListener("direct-upload:error", (event) => {
const { error, file } = event.detail;
console.error(`Upload failed for ${file.name}:`, error);
// Prevent default alert
event.preventDefault();
// Show custom error message
showCustomError(`Failed to upload ${file.name}: ${error}`);
});
// Track form-level completion
document.addEventListener("direct-uploads:end", (event) => {
console.log("All uploads completed, form will now submit");
});For automatic processing, file inputs must:
data-direct-upload-url attribute pointing to the Rails direct upload endpointdisabled attribute)Example with Rails helper:
<%= form_with model: @post do |form| %>
<%= form.file_field :attachments,
multiple: true,
direct_upload: true %>
<%= form.submit %>
<% end %>This generates:
<input type="file"
name="post[attachments][]"
data-direct-upload-url="/rails/active_storage/direct_uploads"
multiple>During uploads, the form enters a processing state:
data-direct-uploads-processing attribute is added to the formCSS Styling:
/* Style forms during processing */
form[data-direct-uploads-processing] {
opacity: 0.7;
pointer-events: none;
}
/* Show loading indicator */
form[data-direct-uploads-processing]::after {
content: "Uploading files...";
position: absolute;
/* Additional styling */
}The automatic integration provides default error handling:
Custom Error Handling:
document.addEventListener("direct-upload:error", (event) => {
// Prevent default alert
event.preventDefault();
const { error, file } = event.detail;
// Show custom error UI
showToast(`Upload failed: ${file.name}`, "error");
// Log for debugging
console.error("Direct upload error:", error);
});Install with Tessl CLI
npx tessl i tessl/npm-rails--activestorage