DOCS LLMs

Attachments and cloud storage (S3-compatible)

Uploading images and files is one of that features that looks trivial until you realize it's not. And mostly every app requires it: setting up user avatars is maybe one of the most common features. Thankfully, RailsFast comes with everything working out of the box, and you don't need to do mostly anything as long as you've configured your R2 credentials correctly in the Configuration section.

In the demo app that ships with RailsFast, all users can upload and update their avatar. This is done by just one line of code in the User model:

has_one_attached :avatar do |attachable|
    attachable.variant :thumb, resize_to_limit: [100, 100], format: :webp
end

As you can see, you can indicate optional sizes and compression for better asset serving & on-page optimization. RailsFast comes with another two image sizes (variants) configured: :small and :medium, so you get an idea how to use it, but it's quite straightforward.

If you want to add image (or file) attatchments to any other model in your app, just copy what's already working for user avatars, and read the official Active Storage docs.