我有一个电子商务应用程序。 我使用paperclip上传图像。 在我的视图页面中,我使用@product.image.url(:thumb)
来显示缩略图,使用@product.image.url
来显示放大的图像。
如果我想在我的产品表上使用SQL并获取所有URL的列表(缩略图和全尺寸),该怎么做? Paperclip将一些图像信息存储在表中,但不存储URL,因此我不知道要使用哪个字段。
我有一个电子商务应用程序。 我使用paperclip上传图像。 在我的视图页面中,我使用@product.image.url(:thumb)
来显示缩略图,使用@product.image.url
来显示放大的图像。
如果我想在我的产品表上使用SQL并获取所有URL的列表(缩略图和全尺寸),该怎么做? Paperclip将一些图像信息存储在表中,但不存储URL,因此我不知道要使用哪个字段。
你需要按照Paperclip的方式构建URL。这对于每个项目都是定制的。你的SQL服务器可能还需要进行哈希和时间转换。
你可以在Paperclip::Attachment
、Paperclip::Interpolations
以及Paperclip::Storage
中找到相关代码。
我没有SQL的例子,但是一个只使用实体而不依赖于Paperclip的Ruby解决方案可能看起来像这样:
entity = User.first
entity_plural_name = "users"
attachment_plural_name = "pictures"
storage = "https://myapp.s3.amazonaws.com/"
secret = "somer4nd0mhash"
styles = [:min, :max, :original]
styles.each_with_object({}) do |style, aggregator|
hash = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA1.new, secret, "#{entity_plural_name}/#{attachment_plural_name}/#{entity.id}/#{style}/#{entity.picture_updated_at.to_i}")
aggregator[style] = "#{storage}/#{entity_plural_name}/#{style}/#{hash}.jpg"
end
使用 paperclip 6.1.0
path
和url
方法对于每个模型都是完全可定制的,因此它们是动态方法。你需要从你的配置中读取生成代码,并将其转换为 SQL 运算符。 - pierallard