Pre-defined ActiveStorage variants in Rails 7.1
Rails 7.1 adds the ability to use pre-defined variants when calling preview
or representation
on an attachment.
class User < ActiveRecord::Base
has_one_attached :file do |attachable|
attachable.variant :thumb, resize_to_limit: [100, 100]
end
end
<%= image_tag user.file.representation(:thumb) %>
Here, we declare a thumb
variant that can later be referenced—no need to explicitly express the resize_to_limit
directly at the image_tag
call.