惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
WordPress大学
WordPress大学
Y
Y Combinator Blog
I
InfoQ
美团技术团队
罗磊的独立博客
B
Blog RSS Feed
GbyAI
GbyAI
小众软件
小众软件
IT之家
IT之家
Engineering at Meta
Engineering at Meta
Blog — PlanetScale
Blog — PlanetScale
V
V2EX
Last Week in AI
Last Week in AI
酷 壳 – CoolShell
酷 壳 – CoolShell
Jina AI
Jina AI
MyScale Blog
MyScale Blog
博客园 - 聂微东
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
Apple Machine Learning Research
Apple Machine Learning Research
The GitHub Blog
The GitHub Blog
T
The Blog of Author Tim Ferriss

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Pro File Uploads in Rails 8: Speed and Scalability with D...
Zil Norvilis · 2026-06-20 · via DEV Community
Cover image for Pro File Uploads in Rails 8: Speed and Scalability with Direct Uploads

Zil Norvilis

Imagine a user trying to upload a 100MB video or a high-resolution photo to your app. If you use the standard Rails file upload, that file travels from the user's browser to your Rails server, and then your server sends it to S3 or Google Cloud.

This is a terrible way to do it. While that 100MB file is transferring, your Rails worker (Puma) is frozen. It can't handle other users. If three people upload large files at once, your whole app will stop responding.

In 2026, the professional way to handle this is Direct Uploads.

With Direct Uploads, the file goes directly from the user's browser to your cloud storage (S3, R2, etc.). Your Rails server only handles a tiny bit of metadata. It is faster for the user and much safer for your server. Here is how to set it up in Rails 8.

STEP 1: Configure Your Storage

First, make sure you aren't using the local disk for production. You need a cloud provider like AWS S3 or Cloudflare R2.

In your config/storage.yml:

amazon:
  service: S3
  access_key_id: <%= ENV['AWS_ACCESS_KEY_ID'] %>
  secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %>
  region: us-east-1
  bucket: my-app-uploads
  # Crucial for Direct Uploads!
  public: true 

Note: You must configure CORS in your S3/R2 dashboard to allow requests from your domain. If you don't do this, the browser will block the upload.

STEP 2: The Rails Form

Rails makes the backend part incredibly easy. You just add one attribute to your file field: direct_upload: true.

<!-- app/views/users/_form.html.erb -->
<%= form_with(model: user) do |f| %>
  <div class="field">
    <%= f.label :avatar %>
    <%= f.file_field :avatar, direct_upload: true %>
  </div>

  <%= f.submit "Save Profile" %>
<% end %>

When you add direct_upload: true, Rails automatically includes a JavaScript library that handles the "handshake" with S3.

STEP 3: Adding a Progress Bar (The UX Win)

Direct uploads can take a few seconds. If nothing happens on the screen, the user will think your app is broken. We can use the built-in ActiveStorage events to show a beautiful progress bar.

First, add a small piece of HTML to your form:

<div class="upload-progress hidden" id="progress-bar">
  <div class="bg-blue-600 h-2 transition-all" id="progress-fill" style="width: 0%"></div>
</div>

Now, we create a tiny Stimulus controller to watch the upload progress.

// app/javascript/controllers/upload_controller.js
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  connect() {
    this.element.addEventListener("direct-upload:progress", event => {
      const { progress } = event.detail
      const bar = document.getElementById("progress-fill")
      const container = document.getElementById("progress-bar")

      container.classList.remove("hidden")
      bar.style.width = `${progress}%`
    })

    this.element.addEventListener("direct-upload:error", event => {
      alert("Upload failed! Please check your connection.")
    })
  }
}

Attach this to your form: <%= form_with(model: user, data: { controller: "upload" }) do |f| %>.

STEP 4: Why this is the "One-Person" Superpower

As a solo developer, you want to avoid "Scaling Issues" as long as possible.

The traditional upload method requires you to have a large server with lots of RAM to handle big file streams. If your app goes viral, you’ll have to pay for a massive server just to move files around.

By using Direct Uploads:

  1. Cost: You can stay on a tiny $5 VPS because S3 does 99% of the work.
  2. Speed: Users see a progress bar immediately.
  3. Reliability: If your server restarts in the middle of an upload, the upload doesn't necessarily fail because it’s not talking to your server!

Summary

Don't let file uploads slow down your monolith. It takes 5 minutes to switch to Direct Uploads, but it makes your app feel like an enterprise product.

  1. Use Cloud Storage (S3/R2).
  2. Enable CORS on your bucket.
  3. Add direct_upload: true to your form.
  4. Add a Stimulus progress bar for that "premium" feel.

Your Puma threads will thank you, and your users will love the snappy experience.