我有一个电子商务应用程序。我用回形针上传图片。在我的 View 页面中,我使用 @product.image.url(:thumb)
用于缩略图和 @product.image.url
作为缩放图像。
如果我想在我的产品表上使用 SQL 并获取所有 url(拇指和全尺寸)的列表,我该怎么做? Paperclip 在表格中存储了一些图像信息,但没有存储 url,所以我不知道要使用哪个字段。
最佳答案
您需要以与回形针相同的方式构建 URL。这是每个项目的自定义。您的 SQL 服务器可能还需要进行散列和时间转换。
您可以在Paperclip::Attachment
中找到相关代码, Paperclip::Interpolations
可能还有Paperclip::Storage
中的一些模块.
我没有 SQL 示例,但 Ruby 中仅使用实体且不依赖 Paperclip 的解决方案可能如下所示:
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
关于ruby-on-rails - Rails - 使用 SQL 获取回形针 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29703342/