【问题标题】:How to get url of Active Storage image如何获取 Active Storage 图片的 url
【发布时间】:2018-11-19 09:40:06
【问题描述】:

我想通过 api 获取带有附加图像的记录列表作为链接或文件。

我有一个简单的模型:

class Category < ApplicationRecord
  has_one_attached :image
  validates :name, presence: true, uniqueness: true
end

下一步行动:

  def index
    @categories = Category.all.with_attached_image

    render json: @categories.to_json(include: { image_attachment: { include: :blob } })
  end

这是我获得图像对象的唯一方法。

我看到了下一个结果:

{"id":4,"name":"Cat1","description":""},
{"id":1,"name":"Cat2","description":"","image_attachment":
  {"id":8,"name":"image","record_type":"Category","record_id":1,"blob_id":8,"created_at":"2018-06-09T13:45:40.512Z","blob":
  {"id":8,"key":"3upLhH4vGxZEhhf3TaAjDiCW","filename":"Screen Shot 2018-06-09 at 20.43.24.png","content_type":"image/png","metadata":
  {"identified":true,"width":424,"height":361,"analyzed":true},"byte_size":337347,"checksum":"Y58zbYUVOlZRadx81wxOJA==","created_at":"2018-06-09T13:45:40.482Z"}}},
...

我可以在这里看到文件名。但是文件存在于不同的文件夹中,对我来说这似乎不是获取和链接到文件的便捷方式。

我找不到任何关于此的信息。

更新

根据 iGian 解决方案,我的代码变为:

  def index
    @categories = Category.all.with_attached_image

    render json: @categories.map { |category|
      category.as_json.merge({ image: url_for(category.image) })
    }
  end

【问题讨论】:

  • 我有一个带有头像作为附件的模型用户,我可以使用&lt;%= image_tag url_for(user.avatar) %&gt; 在我的视图中获取 url,也许你可以只使用url_for(user.avatar)
  • 是的,可以,谢谢!
  • 很好,很高兴为您提供帮助!

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


【解决方案1】:

对于我的Userhas_one_attached :avatar,我可以使用&lt;%= image_tag url_for(user.avatar) %&gt; 在我的视图中获取网址。 所以,在控制器中我只使用url_for(user.avatar)

对于Category 哪个has_one_attached :image

url_for(category.image)

【讨论】:

  • 如何从Image_urlvice-versa获取Image完整数据
  • 如果您需要完整路径,您可以使用polymorphic_url(category.image),其中包括https://...,而不仅仅是路径。
  • 试试main_app.url_for(category.image)
  • 我只想补充一点,如果您不是从视图中访问它,您可以这样做:Rails.application.routes.url_helpers.url_for(category.image)
【解决方案2】:

请按照此获取图像(以防您使用 has_many_attached

Model.images.map{|img| ({ image: url_for(img) })}

【讨论】:

  • 恐怕我无法仅使用答案中的行将单个附件转换为多个附件。我得到的要么只是响应中的“图像”数组,要么是一个错误,这意味着我做错了什么。你能把这个答案放到原始发帖人的“索引”方法块的上下文中吗?
  • 我得到了这个工作,它会是正确的方式,语法? render json: @works.map { |work| work.as_json.merge({ images: work.images.map{|img| ({ image: url_for(img) })} }) }
【解决方案3】:

我让它与rails_blob_url(@object.image) 一起工作。请注意,我使用助手调用 _url 而不是 _path

【讨论】:

    【解决方案4】:

    也可以试试@object.image.service_url。这将为您提供保存图像的 url。 IE。亚马逊 s3 存储的 url。

    【讨论】:

      【解决方案5】:

      这适用于我的多张图片

      class PostSerializer < ActiveModel::Serializer
        include Rails.application.routes.url_helpers
      
        attributes :id, :content , :images
      
        def images
          images = object.images.map do |image|
            rails_blob_path(image , only_path: true) if object.images.attached?
          end
        end
      end
      

      【讨论】:

        【解决方案6】:

        如果你想在前端获取这个 url,试试这个:

        <%= url_for(category.image) %>
        

        以及用于显示图像:

        <%= image_tag url_for(category.image) %>
        

        【讨论】:

          【解决方案7】:

          以下是我如何将主动存储与 AWS S3 存储桶一起使用并将带域的图像 URL 附加到 JSON 响应:

          活动记录

          
          class Imageable < ApplicationRecord
            has_many_attached :images
          
            def image_urls
              images.map(&:service_url)
            end
          
            def attributes
              super.merge({
                            image_urls: image_urls
                          })
            end
          end
          

          application_controller.rb

          class ApplicationController < ActionController::Base
            before_action :set_active_storage_current_host
            
            def set_active_storage_current_host
              ActiveStorage::Current.host = request.base_url
            end
          end
          

          imageables_controller.rb

          class ImageablesController < ApplicationController
            include ImageablesHelper
            def update
              imageable = find_or_create
              imageable.update imageables_params
              imageable.images.purge
              imageable.images.attach imageable_params[:images]
              imageable.save!
              render json: imageable
            end
          end
          

          imageables_helper.rb

          module ImageablesHelper
            def imageables_params
              params.require(:imageable).permit(:other_attributes, images: [])
            end
          end
          

          【讨论】:

            猜你喜欢
            • 2020-03-01
            • 2020-11-27
            • 2020-04-18
            • 2020-09-03
            • 2018-12-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-09-17
            相关资源
            最近更新 更多