简体   繁体   English

回形针,Cloudinary,开发,生产

[英]Paperclip, Cloudinary, Development, Production

What is an elegant solution to having image files on your local disk in development and on Cloudinary in production? 在开发中将本地磁盘上的映像文件和生产中Cloudinary上的映像文件完美解决方案是什么? I have 我有

%td= image_tag investor.image.file.url(cloudinary: {:width => 100, :height => 100, :crop => 'thumb', :gravity => 'face'})

Which look great in production, but it messes up the URL in development. 在生产中看起来不错,但在开发中却弄乱了URL。

%td= image_tag investor.image.file.url(:original, cloudinary: {:width => 100, :height => 100, :crop => 'thumb', :gravity => 'face'})

Also looks good in production, but is much too big in development, as it uses the original file with original dimensions. 在生产上也看起来不错,但是在开发中太大了,因为它使用具有原始尺寸的原始文件。

%td= image_tag investor.image.file.url(:thumb, cloudinary: {:width => 100, :height => 100, :crop => 'thumb', :gravity => 'face'})

Looks good in development, but the thumbnail is too far away and the face is too small in production. 在开发中看起来不错,但缩略图距离太远,生产中的脸孔太小。

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") 

I can't set the default options in the model because the model is used to attach images for two different other models, not just users. 我无法在模型中设置默认选项,因为该模型用于附加两个其他模型的图像,而不仅仅是用户。 The other model that has images doesn't have faces. 另一个具有图像的模型没有面孔。 I'd rather not have to test Rails.env in all my views. 我宁愿不必在所有视图中测试Rails.env

References: 参考文献:

https://github.com/thoughtbot/paperclip https://github.com/thoughtbot/paperclip
http://www.rubydoc.info/gems/paperclip/Paperclip http://www.rubydoc.info/gems/paperclip/Paperclip
https://github.com/GoGoCarl/paperclip-cloudinary https://github.com/GoGoCarl/paperclip-cloudinary
http://cloudinary.com/documentation/rails_integration http://cloudinary.com/documentation/rails_integration

I created a helper. 我创建了一个助手。

  def user_image_url_parameters
    if Rails.env == 'production'
      {cloudinary: {:width => 100, :height => 100, :crop => 'thumb', :gravity => 'face'}}
    else
      :thumb
    end
  end

So the view is 所以视图是

image_tag user.image.file.url(user_image_url_parameters)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM