【问题标题】:How to display the variant of an image with Active Storage in a JS box?如何在 JS 框中显示带有 Active Storage 的图像变体?
【发布时间】:2018-12-07 11:16:42
【问题描述】:

在我看来,我可以像这样在弹出窗口中显示附加到带有 Active Storage 的模型的文件:

<a href="<%= rails_blob_path(@image.file, disposition: 'inline') %>" rel="example_group"><%= image_tag @image.variant('small') %></a>

效果很好。

问题是当我想在链接中使用变体时:

<a href="<%= url_for(@image.variant('high')) %>" rel="example_group"><%= image_tag @image.variant('small') %></a>

使用的变体代码是:

file.variant(resize:size).processed.service_url

链接看起来不错,但是当我单击图像时,图像并没有像以前那样在我的 JS 弹出窗口中打开,而是在新的浏览器窗口中打开。这很奇怪。

我缩短了链接。

https://bucket.s3.eu-west-3.amazonaws.com/variants/MmsLY3rf8yR9/38a77a69d170464c472f6d36fb3fbc28b284af0cadaa533?response-content-disposition=inline%3B%20filename%3D%22chateau.jpeg%22%3B%20filename%2A%3DUTF-8%27%27chateau-lynch.jpeg&response-content-type=image%2Fjpeg&Signature=29fe7d85fe369ea2335fa8b333d4868d8c2f2c22e1efe

这是一个“内容处置”问题吗?

【问题讨论】:

    标签: ruby-on-rails variant rails-activestorage


    【解决方案1】:

    好吧,这就是我所做的:

    在我的图像模型中,我添加了一个动作并使用了来自 url_helpers 的 rails_representation_url() 方法:

    include Rails.application.routes.url_helpers # 需要这个

      include Rails.application.routes.url_helpers 
      def get_variant(version="high", disposition="attachment")
        variant = file_variant(version)
        return rails_representation_url(variant, only_path: true, disposition: disposition)
      end
    

    在我的 html 中,我可以使用附件处理调用我的方法:

    <a href="<%= @image.get_variant('high', 'attachment') %>" rel="example_group">
    

    我也可以直接从我的控制器下载图像变体:

    def download
        redirect_to @image.get_variant('high', 'attachment')
    end
    

    如果你只想在浏览器窗口中显示变体,你可以使用'inline':

    redirect_to @image.get_variant('high', 'inline')
    

    不确定这是不是最好的选择,但它确实有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-01
      • 1970-01-01
      • 2020-07-24
      • 2018-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多