Challenge - 5 Problems
Image and File Handling Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
Active Storage direct upload behavior
What will happen when a user uploads a file using Active Storage direct upload in a Rails form with
has_one_attached :avatar configured?Ruby on Rails
<%= form_with model: @user, local: true do |form| %> <%= form.file_field :avatar, direct_upload: true %> <%= form.submit %> <% end %>
Attempts:
2 left
💡 Hint
Think about how direct upload changes the upload timing in Active Storage.
✗ Incorrect
With direct upload enabled, the file is sent directly to the cloud storage service (like Amazon S3) before the form submits. The form then sends the signed blob ID to associate the file with the model.
📝 Syntax
intermediate2:00remaining
Correct syntax for attaching multiple files in Rails Active Storage
Which code snippet correctly attaches multiple images to a model with
has_many_attached :photos?Ruby on Rails
class Product < ApplicationRecord has_many_attached :photos end # In controller or console:
Attempts:
2 left
💡 Hint
For multiple file uploads from forms, Active Storage accepts an array of files directly.
✗ Incorrect
Active Storage handles arrays of uploaded files directly from params. Option C is the correct and idiomatic syntax.
🔧 Debug
advanced2:00remaining
Fixing missing image preview in Rails Active Storage
A developer tries to display an uploaded image preview with
image_tag user.avatar but sees a broken image icon. What is the most likely cause?Ruby on Rails
class User < ApplicationRecord has_one_attached :avatar end # In view: <%= image_tag user.avatar %>
Attempts:
2 left
💡 Hint
Active Storage attachments need to be converted to URLs for image_tag.
✗ Incorrect
Active Storage attachments are not direct URLs. Using url_for converts the attachment to a URL that image_tag can use to display the image.
❓ state_output
advanced2:00remaining
Result of purging an attachment in Rails Active Storage
What happens to the database and storage when you call
user.avatar.purge on a user with an attached avatar?Ruby on Rails
user.avatar.purge
Attempts:
2 left
💡 Hint
Purging means removing both database and storage data immediately.
✗ Incorrect
Calling purge deletes the attachment record from the database and removes the actual file from the storage service right away.
🧠 Conceptual
expert3:00remaining
Understanding Active Storage variants and transformations
Which statement best describes how Active Storage handles image variants when you request
user.avatar.variant(resize_to_limit: [100, 100])?Attempts:
2 left
💡 Hint
Think about when image processing happens in Active Storage.
✗ Incorrect
Active Storage creates image variants lazily when first requested, then caches them to avoid repeated processing.