$npx -y skills add ThibautBaissac/rails_ai_agents --skill active-storage-setupConfigures Active Storage for file uploads with variants and direct uploads. Use when adding file uploads, image attachments, document storage, generating thumbnails, or when user mentions Active Storage, file upload, attachments, or image processing. WHEN NOT: Storing data in da
| 1 | # Active Storage Setup for Rails 8 |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | Active Storage handles file uploads in Rails: |
| 6 | - Cloud storage (S3, GCS, Azure) or local disk |
| 7 | - Image variants (thumbnails, resizing) |
| 8 | - Direct uploads from browser |
| 9 | - Polymorphic attachments |
| 10 | |
| 11 | ## Quick Start |
| 12 | |
| 13 | ```bash |
| 14 | # Install Active Storage (if not already) |
| 15 | bin/rails active_storage:install |
| 16 | bin/rails db:migrate |
| 17 | |
| 18 | # Add image processing |
| 19 | bundle add image_processing |
| 20 | ``` |
| 21 | |
| 22 | ## Configuration |
| 23 | |
| 24 | ### Storage Services |
| 25 | |
| 26 | ```yaml |
| 27 | # config/storage.yml |
| 28 | local: |
| 29 | service: Disk |
| 30 | root: <%= Rails.root.join("storage") %> |
| 31 | |
| 32 | test: |
| 33 | service: Disk |
| 34 | root: <%= Rails.root.join("tmp/storage") %> |
| 35 | |
| 36 | amazon: |
| 37 | service: S3 |
| 38 | access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %> |
| 39 | secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %> |
| 40 | region: eu-west-1 |
| 41 | bucket: <%= Rails.application.credentials.dig(:aws, :bucket) %> |
| 42 | |
| 43 | google: |
| 44 | service: GCS |
| 45 | credentials: <%= Rails.root.join("config/gcs-credentials.json") %> |
| 46 | project: my-project |
| 47 | bucket: my-bucket |
| 48 | ``` |
| 49 | |
| 50 | ### Environment Config |
| 51 | |
| 52 | ```ruby |
| 53 | # config/environments/development.rb |
| 54 | config.active_storage.service = :local |
| 55 | |
| 56 | # config/environments/production.rb |
| 57 | config.active_storage.service = :amazon |
| 58 | ``` |
| 59 | |
| 60 | ## Model Attachments |
| 61 | |
| 62 | ### Single Attachment |
| 63 | |
| 64 | ```ruby |
| 65 | # app/models/user.rb |
| 66 | class User < ApplicationRecord |
| 67 | has_one_attached :avatar |
| 68 | |
| 69 | # With variant defaults |
| 70 | has_one_attached :avatar do |attachable| |
| 71 | attachable.variant :thumb, resize_to_limit: [100, 100] |
| 72 | attachable.variant :medium, resize_to_limit: [300, 300] |
| 73 | end |
| 74 | end |
| 75 | ``` |
| 76 | |
| 77 | ### Multiple Attachments |
| 78 | |
| 79 | ```ruby |
| 80 | # app/models/event.rb |
| 81 | class Event < ApplicationRecord |
| 82 | has_many_attached :photos |
| 83 | |
| 84 | has_many_attached :documents do |attachable| |
| 85 | attachable.variant :preview, resize_to_limit: [200, 200] |
| 86 | end |
| 87 | end |
| 88 | ``` |
| 89 | |
| 90 | ## TDD Workflow |
| 91 | |
| 92 | ``` |
| 93 | Active Storage Progress: |
| 94 | - [ ] Step 1: Add attachment to model |
| 95 | - [ ] Step 2: Write model spec for attachment |
| 96 | - [ ] Step 3: Add validations (type, size) |
| 97 | - [ ] Step 4: Create upload form |
| 98 | - [ ] Step 5: Handle in controller |
| 99 | - [ ] Step 6: Display in views |
| 100 | - [ ] Step 7: Test upload flow |
| 101 | ``` |
| 102 | |
| 103 | ## Testing Attachments |
| 104 | |
| 105 | See [testing.md](references/testing.md) for model specs, factory traits, and request specs. |
| 106 | |
| 107 | Key patterns: |
| 108 | - Use `fixture_file_upload` in request specs |
| 109 | - Define `:with_avatar` factory traits using `after(:build)` |
| 110 | - Test `be_attached` and variant presence in model specs |
| 111 | |
| 112 | ## Validations |
| 113 | |
| 114 | Use the `active_storage_validations` gem for declarative validation, or write manual `validate` methods. See [validations.md](references/validations.md) for both approaches. |
| 115 | |
| 116 | ```ruby |
| 117 | # Gemfile |
| 118 | gem 'active_storage_validations' |
| 119 | ``` |
| 120 | |
| 121 | ## Image Variants |
| 122 | |
| 123 | Define named variants on the model attachment using `resize_to_fill`, `resize_to_limit`, or `resize_to_cover`. See [variants-and-views.md](references/variants-and-views.md) for variant operations, view helpers, and form examples. |
| 124 | |
| 125 | ## Controller and Service Handling |
| 126 | |
| 127 | - Permit `:avatar` for single uploads, `photos: []` for multiple |
| 128 | - Use `purge` to remove attachments, with optional Turbo Stream response |
| 129 | - Use `rails_blob_path` or `send_data` for downloads |
| 130 | |
| 131 | See [controller-and-service.md](references/controller-and-service.md) for full controller examples, service methods, direct uploads setup, and performance tips. |
| 132 | |
| 133 | ## Checklist |
| 134 | |
| 135 | - [ ] Active Storage installed and migrated |
| 136 | - [ ] Storage service configured |
| 137 | - [ ] Image processing gem added (if using variants) |
| 138 | - [ ] Attachment added to model |
| 139 | - [ ] Validations added (type, size) |
| 140 | - [ ] Variants defined |
| 141 | - [ ] Controller permits attachment params |
| 142 | - [ ] Form handles file upload |
| 143 | - [ ] Tests written for attachments |
| 144 | - [ ] Direct uploads configured (if needed) |
| 145 | |
| 146 | ## References |
| 147 | |
| 148 | - [testing.md](references/testing.md) - Model specs, factory traits, request specs |
| 149 | - [validations.md](references/validations.md) - Gem-based and manual validation examples |
| 150 | - [variants-and-views.md](references/variants-and-views.md) - Variant definitions, view helpers, upload forms |
| 151 | - [controller-and-service.md](references/controller-and-service.md) - Controllers, service methods, direct uploads, performance |