Active Storage helps you easily add file uploads to your Rails app. It manages storing files and linking them to your data.
File uploads with Active Storage in Ruby on Rails
class User < ApplicationRecord has_one_attached :avatar end # In a form view: <%= form_with model: @user do |form| %> <%= form.file_field :avatar %> <%= form.submit %> <% end %>
has_one_attached is for one file per record.
Use has_many_attached if you want multiple files.
class Article < ApplicationRecord
has_many_attached :images
end<%= form.file_field :avatar, direct_upload: true %>
user.avatar.attach(params[:user][:avatar])
This example shows a User model with one attached avatar file. The form lets users pick a file to upload. The controller saves the user and attaches the file.
class User < ApplicationRecord has_one_attached :avatar end # Controller example class UsersController < ApplicationController def new @user = User.new end def create @user = User.new(user_params) if @user.save redirect_to @user, notice: "User created with avatar!" else render :new end end private def user_params params.require(:user).permit(:name, :avatar) end end # View (new.html.erb) <%= form_with model: @user, local: true do |form| %> <%= form.label :name %> <%= form.text_field :name %> <%= form.label :avatar %> <%= form.file_field :avatar %> <%= form.submit "Create User" %> <% end %>
Remember to run rails active_storage:install and migrate your database before using Active Storage.
Uploaded files are stored in a service like local disk, Amazon S3, or Google Cloud Storage configured in config/storage.yml.
Use rails server and check browser DevTools Network tab to see file upload requests.
Active Storage makes file uploads easy and secure in Rails.
Use has_one_attached or has_many_attached to link files to models.
Forms use file_field to let users pick files to upload.
Practice
has_one_attached in a Rails model when using Active Storage?Solution
Step 1: Understand Active Storage attachment macros
Thehas_one_attachedmacro in a Rails model declares a one-to-one association between the model and a single uploaded file.Step 2: Differentiate from other options
It does not create tables, validate file types, or resize images by itself; those are handled elsewhere.Final Answer:
To link a single uploaded file to a model instance -> Option AQuick Check:
has_one_attached = single file link [OK]
- Confusing has_one_attached with database table creation
- Thinking it validates or processes files automatically
- Mixing it up with has_many_attached for multiple files
Solution
Step 1: Identify the correct form helper for file uploads
Thefile_fieldhelper creates a file input allowing users to select files for upload.Step 2: Exclude other input types
Text fields, submit buttons, and select dropdowns do not allow file selection.Final Answer:
<%= form.file_field :avatar %> -> Option AQuick Check:
file_field = file input [OK]
- Using text_field instead of file_field for uploads
- Confusing submit button with file input
- Using select dropdown for file selection
class User < ApplicationRecord has_one_attached :profile_pic end # In the form view: <%= form_with model: @user do |form| %> <%= form.file_field :profile_pic %> <%= form.submit %> <% end %>
Solution
Step 1: Check model attachment declaration
The model useshas_one_attached :profile_pic, so it can attach one file to the user.Step 2: Understand form_with default behavior
Rails 6+ automatically sets multipart for file uploads inform_with, so the file will be sent correctly.Final Answer:
The uploaded file will be attached to the user's profile_pic -> Option BQuick Check:
has_one_attached + file_field = file attached [OK]
- Assuming file is saved as string in DB column
- Thinking multipart option must be manually added
- Believing file is ignored without extra code
class Product < ApplicationRecord has_one_attached :images end # In the form: <%= form.file_field :images, multiple: true %>
Solution
Step 1: Check model attachment type
The model useshas_one_attached, which supports only one file, but the form allows multiple files.Step 2: Correct attachment macro for multiple files
To attach multiple files, the model must usehas_many_attached.Final Answer:
has_one_attached should be has_many_attached for multiple files -> Option DQuick Check:
Multiple files need has_many_attached [OK]
- Using has_one_attached with multiple: true
- Thinking file_field can't handle multiple files
- Not declaring attachment in model for uploads
Solution
Step 1: Choose correct model attachment for multiple files
To attach multiple images, usehas_many_attached :photosin the model.Step 2: Use file_field with multiple option in the form
The form must usefile_field :photos, multiple: trueto allow selecting multiple files.Step 3: Display thumbnails after upload
With this setup, you can iterate over@model.photosto show thumbnails.Final Answer:
Model: has_many_attached :photos and View: form.file_field :photos, multiple: true -> Option CQuick Check:
Multiple files need has_many_attached + multiple: true [OK]
- Using has_one_attached for multiple files
- Omitting multiple: true in file_field
- Using text_field instead of file_field for uploads
Ready to prove it?
Pass the test and this pattern moves unknown
