Active Storage and its adapters and plugins
Active Storage is a relatively new addition to Ruby on Rails (added in 5.2). This library aims to satisfy your needs regarding file uploads.
With Active Storage, enhancing a model with a file-backed attribute is as simple as adding a single line of code:
class Post < ApplicationRecord has_one_attached :image end
Then, you can attach a file to a record by simply passing it along with other attributes:
# Now you can create a post with an imageimage = File.open("example.png") post = Post.create!(title: "Test") post.image.attach(io: image, filename: "example.png") # Use a variant of the image somewhere in a template <%= image_tag post.image.variant(resize: "400x300") %>
From an architectural point of view, Active Storage can be divided into four components:
- Uploading and serving files (controllers, helpers, and client libraries)
- Active Record integration (
has_one_attached...