生产环境Active_Storage加载资源失败(500错误)求助
Let's work through your Active Storage issues one by one to get those images loading properly!
1. Fix the "No such file or directory" Fatal Error
First, that Errno::ENOENT error is telling us Active Storage can't find the image file it's trying to serve. Looking at your logs, you're deploying to Heroku—and here's the key detail: Heroku's local file system is temporary. Any files stored in /app/storage get wiped when your app restarts, scales dynos, or you push new code. That's why the file exists briefly (when you upload it) but disappears later.
Quick Fix for Heroku: Switch to Cloud Storage
You can't use Active Storage's local disk service on Heroku. Instead, set up a cloud storage backend like AWS S3, Google Cloud Storage, or Azure:
- Add the appropriate gem to your
Gemfile(e.g., for AWS S3:gem 'aws-sdk-s3', require: false) - Configure your storage service in
config/storage.yml:
amazon: service: S3 access_key_id: <%= ENV['AWS_ACCESS_KEY_ID'] %> secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %> region: <%= ENV['AWS_REGION'] %> bucket: <%= ENV['S3_BUCKET_NAME'] %>
- Update your production environment config (
config/environments/production.rb) to use this service:
config.active_storage.service = :amazon
- Set the required environment variables on Heroku (via the dashboard or
heroku config:setcommand) - Re-deploy and run
heroku run rails db:migrateto ensure Active Storage's tables are set up
For Development Environment
If you're seeing this locally, make sure your storage directory exists and has correct permissions:
mkdir -p storage chmod -R 755 storage
Also, your purge_later logic runs asynchronously—if you try to access an image right after triggering deletion, it might already be gone. Add a check in your view to only render existing images:
<% @project.images.each do |image| %> <% if image.attached? && image.persisted? %> <%= image_tag image %> <% end %> <% end %>
2. Fix the "401 Unauthorized" Eventsource Error
This happens because Active Storage's disk controller requires authentication by default, but you're allowing unauthenticated users to view project pages. When those users try to load images, the server rejects the request with a 401.
To fix this, add a skip for the Active Storage disk controller in your routes or application controller:
Option 1: Update Routes
Add this to config/routes.rb before your other routes:
# Allow unauthenticated access to Active Storage disk files get "/rails/active_storage/disk/:encoded_key/*filename", to: "active_storage/disk#show", as: :rails_disk_service, skip_before_action: :authenticate_user!
Option 2: Update Application Controller
If you prefer, add this to app/controllers/application_controller.rb:
skip_before_action :authenticate_user!, only: [:index, :show, :active_storage_disk_show] def active_storage_disk_show # Empty action to map the route permission end
3. Double-Check Your Setup
- Make sure you ran Active Storage's install and migration commands:
rails active_storage:install rails db:migrate
This creates the active_storage_blobs and active_storage_attachments tables that Active Storage needs to track files.
- Your
project_paramsalready includesimages:[]and:remove_image, which is correct for multiple file uploads and your deletion logic.
Once you implement these changes, your images should persist across Heroku deployments and load without 401 errors for unauthenticated users!
内容的提问来源于stack exchange,提问作者gogodev

