How It Works Features Pricing Blog Error Guides
Log In Start Free Trial
Rails · Ruby

Fix ActiveStorage::FileNotFoundError: ActiveStorage::FileNotFoundError in Rails

This error occurs when ActiveStorage cannot find the blob file in the configured storage service. The file may have been deleted from the storage backend, the storage configuration may be wrong, or the blob record exists in the database but the actual file is missing. Verify your storage.yml configuration and check file existence.

Reading the Stack Trace

ActiveStorage::FileNotFoundError: activestorage (7.1.3) lib/active_storage/service/disk_service.rb:142:in `stream' activestorage (7.1.3) lib/active_storage/blobs/proxy_controller.rb:17:in `show' actionpack (7.1.3) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action' actionpack (7.1.3) lib/abstract_controller/base.rb:224:in `process_action' activestorage (7.1.3) lib/active_storage/service/disk_service.rb:25:in `download'

Here's what each line means:

Common Causes

1. File deleted from storage

The physical file was deleted from the storage directory but the blob record still exists in the database.

# The blob record exists:
# ActiveStorage::Blob.find_by(key: 'abc123') => #<Blob key: 'abc123'>
# But the file is missing:
# storage/ab/c1/abc123 => File not found

2. Wrong storage service configuration

The storage.yml points to a different directory or S3 bucket than where files were uploaded.

# config/storage.yml
local:
  service: Disk
  root: <%= Rails.root.join('storage') %>
# But files were uploaded to tmp/storage previously

3. Missing environment config

The environment does not specify which storage service to use.

# config/environments/production.rb
# config.active_storage.service = :amazon  # This line is missing

The Fix

Ensure the storage.yml configuration points to the correct storage root directory and configure a durable cloud storage service like S3 for production to prevent file loss.

Before (broken)
# config/storage.yml
local:
  service: Disk
  root: <%= Rails.root.join('tmp/storage') %>
After (fixed)
# config/storage.yml
local:
  service: Disk
  root: <%= Rails.root.join('storage') %>

amazon:
  service: S3
  access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %>
  secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %>
  region: us-east-1
  bucket: myapp-production

Testing the Fix

require 'rails_helper'

RSpec.describe 'ActiveStorage attachments', type: :model do
  describe User do
    it 'attaches an avatar' do
      user = create(:user)
      user.avatar.attach(
        io: File.open(Rails.root.join('spec/fixtures/files/avatar.png')),
        filename: 'avatar.png',
        content_type: 'image/png'
      )
      expect(user.avatar).to be_attached
    end

    it 'downloads the attached file' do
      user = create(:user, :with_avatar)
      expect(user.avatar.download).to be_present
    end
  end
end

Run your tests:

bundle exec rspec spec/models/user_active_storage_spec.rb

Pushing Through CI/CD

git checkout -b fix/rails-active-storage-config,git add config/storage.yml config/environments/production.rb,git commit -m "fix: correct ActiveStorage service configuration",git push origin fix/rails-active-storage-config

Your CI config should look something like this:

name: CI
on:
  pull_request:
    branches: [main]
jobs:
  test:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:16
        env:
          POSTGRES_PASSWORD: postgres
        ports: ['5432:5432']
    steps:
      - uses: actions/checkout@v4
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: '3.3'
          bundler-cache: true
      - run: bin/rails db:setup
      - run: bundle exec rspec

The Full Manual Process: 18 Steps

Here's every step you just went through to fix this one bug:

  1. Notice the error alert or see it in your monitoring tool
  2. Open the error dashboard and read the stack trace
  3. Identify the file and line number from the stack trace
  4. Open your IDE and navigate to the file
  5. Read the surrounding code to understand context
  6. Reproduce the error locally
  7. Identify the root cause
  8. Write the fix
  9. Run the test suite locally
  10. Fix any failing tests
  11. Write new tests covering the edge case
  12. Run the full test suite again
  13. Create a new git branch
  14. Commit and push your changes
  15. Open a pull request
  16. Wait for code review
  17. Merge and deploy to production
  18. Monitor production to confirm the error is resolved

Total time: 30-60 minutes. For one bug.

Or Let bugstack Fix It in Under 2 minutes

Every step above? bugstack does it automatically.

Step 1: Install the SDK

gem install bugstack

Step 2: Initialize

require 'bugstack'

Bugstack.init(api_key: ENV['BUGSTACK_API_KEY'])

Step 3: There is no step 3.

bugstack handles everything from here:

  1. Captures the stack trace and request context
  2. Pulls the relevant source files from your GitHub repo
  3. Analyzes the error and understands the code context
  4. Generates a minimal, verified fix
  5. Runs your existing test suite
  6. Pushes through your CI/CD pipeline
  7. Deploys to production (or opens a PR for review)

Time from error to fix deployed: Under 2 minutes.

Human involvement: zero.

Try bugstack Free →

No credit card. 5-minute setup. Cancel anytime.

Deploying the Fix (Manual Path)

  1. Verify storage.yml has the correct configuration.
  2. Set the active_storage.service in each environment config.
  3. Run ActiveStorage specs.
  4. Open a pull request.
  5. Merge and verify file uploads and downloads work in staging.

Frequently Asked Questions

BugStack runs the fix through your existing test suite, generates additional edge-case tests, and validates that no other components are affected before marking it safe to deploy.

BugStack never pushes directly to production. Every fix goes through a pull request with full CI checks, so your team can review it before merging.

Use ActiveStorage::Blob.find_each and upload each blob to S3 with the same key. The active_storage_blobs table tracks all files.

Yes, Rails 6.1+ supports multiple services per model. Use has_one_attached :file, service: :amazon to specify which service each attachment uses.