【发布时间】:2019-11-23 07:34:37
【问题描述】:
我正在尝试预览我上传的 PDF 文件和图像文件:
class Document < ApplicationRecord
belongs_to :resourceable, :polymorphic => true
has_one_attached :file
end
我尝试使用 .preview 和 .representation 方法将上传的图像或 pdf 的预览显示为缩略图。
- 使用 preview(),会导致以下错误:
@document.file.preview(resize_to_limit: [100, 100])
ActiveStorage::UnpreviewableError: ActiveStorage::UnpreviewableError
- 使用representation(),image_tag 无法从ActiveStorage::Variant 对象解析图像url,导致以下错误:
>>> image_tag(@document.file.representation(resize: '500x500'))
ArgumentError: Can't resolve image into URL: undefined method `to_model' for #<ActiveStorage::Variant:0x00007fe39e809768>
document.file.representable? 和 document.file.previewable? 在某些文档中。想知道这些方法有什么作用?有时我可以将其表示为 true,但可以预览为 false。
【问题讨论】: