【问题标题】:Rails ActiveStorage: How to preview image stored in active storage?Rails ActiveStorage:如何预览存储在活动存储中的图像?
【发布时间】:2021-08-20 04:53:00
【问题描述】:

我正在使用 ActiveStorage 存储帖子的图像。我的模型是这样的

class Post
  has_many_attached :images
end

我可以将图像保存在活动存储中。但我无法预览它们。我能够显示下载链接,然后查看图像。我的代码是这样的

<% @post.images.each do |image| %>
  <%= link_to "View", image %>
<% end %>

但我想显示图片而不是下载链接。那么如何预览图像呢?我尝试了类似的方法,但没有成功

<% @post.images.each do |image| %>
  <%= image_tag(image) %>
<% end %>

我也尝试安装poppler gem 并尝试这样,但这也没有用。

<%= image_tag image.preview(resize_to_limit: [100, 100]) %>

更新:

&lt;%= image_tag(image) %&gt; 工作。我无法查看图像,因为图像太大,所以我不得不向下滚动。

【问题讨论】:

  • 你能检查图像文件的url(src属性)和下载链接(href)是否相同?此外,如果您可以分享您遇到的错误或者如果您没有收到任何错误,那么这两种情况的结果 html 是什么(链接与 image_tag)
  • @Ankit 在使用&lt;%= image_tag image.preview(resize_to_limit: [100, 100]) 时,我得到ActiveStorage::UnpreviewableError

标签: ruby-on-rails rails-activestorage


【解决方案1】:

&lt;%= image_tag(image) %&gt; 工作。我无法查看图像,因为图像太大,所以我不得不向下滚动。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-20
    • 2021-09-25
    • 1970-01-01
    • 2018-11-18
    • 1970-01-01
    相关资源
    最近更新 更多