【发布时间】:2017-10-14 01:49:27
【问题描述】:
在开发中将图像文件保存在本地磁盘上,在生产中将图像文件保存在 Cloudinary 上,有什么优雅的解决方案?我有
%td= image_tag investor.image.file.url(cloudinary: {:width => 100, :height => 100, :crop => 'thumb', :gravity => 'face'})
这在生产中看起来很棒,但在开发中会弄乱 URL。
%td= image_tag investor.image.file.url(:original, cloudinary: {:width => 100, :height => 100, :crop => 'thumb', :gravity => 'face'})
在生产中看起来也不错,但在开发中太大了,因为它使用具有原始尺寸的原始文件。
%td= image_tag investor.image.file.url(:thumb, cloudinary: {:width => 100, :height => 100, :crop => 'thumb', :gravity => 'face'})
在开发中看起来不错,但是缩略图太远了,在生产中人脸太小了。
model
class Image < ApplicationRecord
if Rails.env == 'production'
has_attached_file :file, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: ActionController::Base.helpers.asset_path("user.png"),
:storage => :cloudinary,
:path => ':class/:id/:style/:filename'
else
has_attached_file :file, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: ActionController::Base.helpers.asset_path("user.png")
我无法在模型中设置默认选项,因为该模型用于为两个不同的其他模型附加图像,而不仅仅是用户。另一个有图像的模型没有面孔。我宁愿不必在所有观点中测试Rails.env。
参考资料:
https://github.com/thoughtbot/paperclip
http://www.rubydoc.info/gems/paperclip/Paperclip
https://github.com/GoGoCarl/paperclip-cloudinary
http://cloudinary.com/documentation/rails_integration
【问题讨论】:
标签: ruby-on-rails heroku paperclip cloudinary