如何获取 url 个 Active Storage 映像
How to get url of Active Storage image
我想通过 api.
获取带有附加图像的记录列表作为 links 或文件
我有一个简单的模型:
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"}}},
...
我可以在这里看到文件名。但是文件位于不同的文件夹中,对我来说这似乎不是获取和 link 文件的便捷方式。
我找不到任何关于此的信息。
已更新
根据 iGian 解决方案,我的代码变为:
def index
@categories = Category.all.with_attached_image
render json: @categories.map { |category|
category.as_json.merge({ image: url_for(category.image) })
}
end
对于我的 User
has_one_attached :avatar
我可以用 <%= image_tag url_for(user.avatar) %>
在我的观点中得到 url。
所以,在控制器中我只使用 url_for(user.avatar)
对于 Category
其中 has_one_attached :image
:
url_for(category.image)
请按照此获取图像(如果您使用的是 has_many_attached)
Model.images.map{|img| ({ image: url_for(img) })}
我让它与 rails_blob_url(@object.image)
一起工作。请注意,我正在使用助手调用 _url 而不是 _path。
也试试 @object.image.service_url
。这将为您提供保存图像的 url。 IE。 url 到亚马逊 s3 存储。
这对我来说适用于多张图片
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
如果你想在前端得到这个url,试试这个:
<%= url_for(category.image) %>
并用于显示图像:
<%= image_tag url_for(category.image) %>
以下是我如何将活动存储与 AWS S3 存储桶一起使用并将带有域的图像 url 附加到 JSON 响应:
activerecord
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
我想通过 api.
获取带有附加图像的记录列表作为 links 或文件我有一个简单的模型:
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"}}},
...
我可以在这里看到文件名。但是文件位于不同的文件夹中,对我来说这似乎不是获取和 link 文件的便捷方式。
我找不到任何关于此的信息。
已更新
根据 iGian 解决方案,我的代码变为:
def index
@categories = Category.all.with_attached_image
render json: @categories.map { |category|
category.as_json.merge({ image: url_for(category.image) })
}
end
对于我的 User
has_one_attached :avatar
我可以用 <%= image_tag url_for(user.avatar) %>
在我的观点中得到 url。
所以,在控制器中我只使用 url_for(user.avatar)
对于 Category
其中 has_one_attached :image
:
url_for(category.image)
请按照此获取图像(如果您使用的是 has_many_attached)
Model.images.map{|img| ({ image: url_for(img) })}
我让它与 rails_blob_url(@object.image)
一起工作。请注意,我正在使用助手调用 _url 而不是 _path。
也试试 @object.image.service_url
。这将为您提供保存图像的 url。 IE。 url 到亚马逊 s3 存储。
这对我来说适用于多张图片
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
如果你想在前端得到这个url,试试这个:
<%= url_for(category.image) %>
并用于显示图像:
<%= image_tag url_for(category.image) %>
以下是我如何将活动存储与 AWS S3 存储桶一起使用并将带有域的图像 url 附加到 JSON 响应:
activerecord
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