0
0
Ruby on Railsframework~10 mins

Image and file handling in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Image and file handling
User uploads file
Rails receives file in params
Attach file to model using Active Storage
File saved in storage service
File accessible via URL or download
Display or process file in views/controllers
This flow shows how Rails handles user file uploads by receiving the file, attaching it to a model with Active Storage, saving it, and then making it accessible for display or download.
Execution Sample
Ruby on Rails
class User < ApplicationRecord
  has_one_attached :avatar
end

# In controller
@user.avatar.attach(params[:avatar])
This code attaches an uploaded avatar image file to a User model instance using Active Storage.
Execution Table
StepActionInput/StateResult/State Change
1User submits form with fileparams[:avatar] = uploaded fileFile data received in params
2Controller calls attach@user.avatar.attach(params[:avatar])File attached to @user's avatar
3Active Storage saves fileFile attachedFile saved in configured storage (local/cloud)
4File metadata savedAttachment infoDatabase records created for attachment
5File accessible via URL@user.avatarURL generated for file access
6View renders image tag<%= image_tag @user.avatar %>Image displayed in browser
7User requests file downloadURL accessedFile served from storage
8File deleted if detached@user.avatar.purgeFile removed from storage and DB
9EndNo further actionProcess complete
💡 File is saved and accessible until explicitly removed or model is destroyed
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
params[:avatar]niluploaded file objectuploaded file objectuploaded file objectuploaded file object
@user.avatar.attached?falsetruetruetruetrue
Storage file presencenonenonefile savedfile savedfile saved
DB attachment recordnonenonenonecreatedcreated
Key Moments - 3 Insights
Why does the file not appear immediately after attach is called?
Because attach queues the file to be saved, but the actual file saving happens asynchronously or after the request completes, as shown in steps 2 and 3 of the execution_table.
What happens if you call purge on the attachment?
Calling purge removes the file from storage and deletes the attachment record from the database, as shown in step 8 of the execution_table.
How does Rails know where to save the file?
Rails uses the configured Active Storage service (local disk, Amazon S3, etc.) defined in config/storage.yml, which is referenced during the file save step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the file actually saved to storage?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Check the 'Result/State Change' column for when the file is saved in storage.
According to variable_tracker, when does @user.avatar.attached? become true?
AAfter Step 1
BAfter Step 3
CAfter Step 2
DAfter Step 4
💡 Hint
Look at the @user.avatar.attached? row and see when it changes from false to true.
If you remove the file using purge, which step in execution_table shows this action?
AStep 8
BStep 6
CStep 7
DStep 9
💡 Hint
Look for the step mentioning file deletion or purge.
Concept Snapshot
Rails Image and File Handling with Active Storage:
- Use has_one_attached or has_many_attached in models
- Attach files via model_instance.attach(params[:file])
- Files saved in configured storage (local/cloud)
- Access files with model_instance.file
- Remove files with model_instance.file.purge
- Display files in views with image_tag or link_to
Full Transcript
In Rails, image and file handling is done using Active Storage. When a user uploads a file, it is received in the controller through params. The file is then attached to a model instance using the attach method. Active Storage saves the file in the configured storage service, such as local disk or cloud storage. Metadata about the attachment is saved in the database. The file can be accessed via URLs generated by Active Storage and displayed in views using helpers like image_tag. Files can be deleted by calling purge on the attachment. This process ensures files are managed efficiently and securely within Rails applications.