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
Here's what each line means:
- activestorage (7.1.3) lib/active_storage/service/disk_service.rb:142:in `stream': The disk service cannot locate the file at the expected path in the storage directory.
- activestorage (7.1.3) lib/active_storage/blobs/proxy_controller.rb:17:in `show': The blob proxy controller is trying to serve the file to the client.
- activestorage (7.1.3) lib/active_storage/service/disk_service.rb:25:in `download': The download method fails because the underlying file does not exist on disk.
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.
# config/storage.yml
local:
service: Disk
root: <%= Rails.root.join('tmp/storage') %>
# 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:
- Notice the error alert or see it in your monitoring tool
- Open the error dashboard and read the stack trace
- Identify the file and line number from the stack trace
- Open your IDE and navigate to the file
- Read the surrounding code to understand context
- Reproduce the error locally
- Identify the root cause
- Write the fix
- Run the test suite locally
- Fix any failing tests
- Write new tests covering the edge case
- Run the full test suite again
- Create a new git branch
- Commit and push your changes
- Open a pull request
- Wait for code review
- Merge and deploy to production
- 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:
- Captures the stack trace and request context
- Pulls the relevant source files from your GitHub repo
- Analyzes the error and understands the code context
- Generates a minimal, verified fix
- Runs your existing test suite
- Pushes through your CI/CD pipeline
- 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)
- Verify storage.yml has the correct configuration.
- Set the active_storage.service in each environment config.
- Run ActiveStorage specs.
- Open a pull request.
- 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.