如何获取Active Storage映像的网址

时间:2018-06-09 15:11:31

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

我希望通过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

7 个答案:

答案 0 :(得分:12)

对于我的User has_one_attached :avatar我可以使用<%= image_tag url_for(user.avatar) %>获取我的观看中的网址。 所以,在控制器中,我只使用url_for(user.avatar)

对于Category:{/ 1> has_one_attached :image

url_for(category.image)

答案 1 :(得分:4)

也尝试@object.image.service_url。这将为您提供保存图像的URL。即网址到Amazon s3存储。

答案 2 :(得分:1)

请遵循以下步骤来获取图像(如果使用的是 has_many_attached

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

答案 3 :(得分:1)

我知道它可以与rails_blob_url(@object.image)一起使用。请注意,我正在通过助手呼叫 _url 而不是 _path

答案 4 :(得分:1)

这对我来说适用于多张图片

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

答案 5 :(得分:0)

如果您想在前端获取此网址,请尝试以下方法:

    sudo iptables-save | grep nginx
    
    -A KUBE-NODEPORTS -p tcp -m comment --comment "default/nginx:" -m tcp --dport 31554 -j KUBE-SVC-4N57TFCL4MD7ZTDA    //KUBE-SVC-4N57TFCL4MD7ZTDA is a tag for nginx service
    
    sudo iptables-save | grep KUBE-SVC-4N57TFCL4MD7ZTDA
    -A KUBE-SVC-4N57TFCL4MD7ZTDA -m statistic --mode random --probability 0.50000000000 -j KUBE-SEP-SOWYYRHSSTWLCRDY

并用于显示图像:

<%= url_for(category.image) %>

答案 6 :(得分:0)

以下是我如何将主动存储与 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