module ActiveStorage::Blob::Representable
def preview(transformations)
This method raises ActiveStorage::UnpreviewableError if no previewer accepts the receiving blob. To determine
<%= image_tag video.preview(resize: "100x100") %>
how to use the built-in version:
Active Storage provides one, but you may want to create your own (for example, if you need authentication). Here’s
Avoid processing previews synchronously in views. Instead, link to a controller action that processes them on demand.
blob.preview(resize: "100x100").processed.service_url
extracts the first frame from a video and the PDF previewer extracts the first page from a PDF document.
from a non-image blob. Active Storage comes with built-in previewers for videos and PDF documents. The video previewer
Returns an ActiveStorage::Preview instance with the set of +transformations+ provided. A preview is an image generated
def preview(transformations) if previewable? ActiveStorage::Preview.new(self, transformations) else raise ActiveStorage::UnpreviewableError end end
def previewable?
def previewable? ActiveStorage.previewers.any? { |klass| klass.accept?(self) } end
def representable?
def representable? variable? || previewable? end
def representation(transformations)
ActiveStorage::Blob#representable? to determine whether a blob is representable.
Raises ActiveStorage::UnrepresentableError if the receiving blob is neither variable nor previewable. Call
blob.representation(resize: "100x100").processed.service_url
Returns an ActiveStorage::Preview for a previewable blob or an ActiveStorage::Variant for a variable image blob.
def representation(transformations) case when previewable? preview transformations when variable? variant transformations else raise ActiveStorage::UnrepresentableError end end
def variable?
def variable? ActiveStorage.variable_content_types.include?(content_type) end
def variant(transformations)
Raises ActiveStorage::InvariableError if ImageMagick cannot transform the blob. To determine whether a blob is
can then produce on-demand.
This will create a URL for that specific blob with that specific variant, which the ActiveStorage::RepresentationsController
<%= image_tag Current.user.avatar.variant(resize: "100x100") %>
specific variant that can be created by a controller on-demand. Like so:
Frequently, though, you don't actually want to transform the variant right away. But rather simply refer to a
Then it'll upload said variant to the service according to a derivative key of the blob and the transformations.
This will create and process a variant of the avatar blob that's constrained to a height and width of 100px.
avatar.variant(resize: "100x100").processed.service_url
files, and it allows any image to be transformed for size, colors, and the like. Example:
Returns an ActiveStorage::Variant instance with the set of +transformations+ provided. This is only relevant for image
def variant(transformations) if variable? ActiveStorage::Variant.new(self, transformations) else raise ActiveStorage::InvariableError end end