[英]Paperclip, Cloudinary, Development, Production
在开发中将本地磁盘上的映像文件和生产中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
我创建了一个助手。
def user_image_url_parameters
if Rails.env == 'production'
{cloudinary: {:width => 100, :height => 100, :crop => 'thumb', :gravity => 'face'}}
else
:thumb
end
end
所以视图是
image_tag user.image.file.url(user_image_url_parameters)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.