0
0
Ruby on Railsframework~20 mins

Image and file handling in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Image and File Handling Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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 %>
AThe file is uploaded directly to the cloud storage before the form is submitted, then the form submits the signed blob ID.
BThe file is uploaded only after the form is submitted, as part of the controller's create action.
CThe file is not uploaded at all because direct_upload: true disables uploads.
DThe file is uploaded to a temporary local folder and then moved to cloud storage after form submission.
Attempts:
2 left
💡 Hint
Think about how direct upload changes the upload timing in Active Storage.
📝 Syntax
intermediate
2: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:
Aproduct.photos.attach(params[:photos].map { |p| { io: p, filename: p.original_filename } })
Bproduct.photos.attach([params[:photos]])
Cproduct.photos.attach(params[:photos])
Dproduct.photos.attach(io: params[:photos], filename: 'image.jpg')
Attempts:
2 left
💡 Hint
For multiple file uploads from forms, Active Storage accepts an array of files directly.
🔧 Debug
advanced
2: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 %>
AThe image_tag helper needs a URL, so it should be <code>image_tag url_for(user.avatar)</code>.
BThe avatar attachment is missing from the database, so the user must re-upload the image.
CActive Storage is not installed, so image_tag cannot display attachments.
DThe image_tag helper only works with local files, not Active Storage attachments.
Attempts:
2 left
💡 Hint
Active Storage attachments need to be converted to URLs for image_tag.
state_output
advanced
2: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
AOnly the database record is deleted; the file remains in storage until manually removed.
BThe attachment record is deleted from the database and the file is removed from storage immediately.
CThe file is deleted from storage but the database record remains until the user is deleted.
DNothing happens because purge only marks the attachment for deletion later.
Attempts:
2 left
💡 Hint
Purging means removing both database and storage data immediately.
🧠 Conceptual
expert
3: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])?
AThe variant is generated on the client side using JavaScript after the image loads.
BActive Storage pre-generates all variants when the original image is uploaded.
CVariants are stored as separate attachments linked to the original image.
DActive Storage generates the resized image variant on demand and caches it for future requests.
Attempts:
2 left
💡 Hint
Think about when image processing happens in Active Storage.