instance method
variant
Ruby on Rails 6.1.7.10
Since v5.2.8.1Signature
variant(transformations)
Returns an ActiveStorage::Variant instance with the set of transformations provided. This is only relevant for image files, and it allows any image to be transformed for size, colors, and the like. Example:
avatar.variant(resize_to_limit: [100, 100]).processed.url
This will create and process a variant of the avatar blob that’s constrained to a height and width of 100px. Then it’ll upload said variant to the service according to a derivative key of the blob and the transformations.
Frequently, though, you don’t actually want to transform the variant right away. But rather simply refer to a specific variant that can be created by a controller on-demand. Like so:
<%= image_tag Current.user.avatar.variant(resize_to_limit: [100, 100]) %>
This will create a URL for that specific blob with that specific variant, which the ActiveStorage::RepresentationsController can then produce on-demand.
Raises ActiveStorage::InvariableError if ImageMagick cannot transform the blob. To determine whether a blob is variable, call ActiveStorage::Blob#variable?.
Parameters
-
transformationsreq
Source
# File activestorage/app/models/active_storage/blob/representable.rb, line 33
def variant(transformations)
if variable?
variant_class.new(self, ActiveStorage::Variation.wrap(transformations).default_to(default_variant_transformations))
else
raise ActiveStorage::InvariableError
end
end
Defined in activestorage/app/models/active_storage/blob/representable.rb line 33
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveStorage::Blob::Representable