【问题标题】:Paperclip, Cloudinary, Development, Production回形针, Cloudinary, 开发, 生产
【发布时间】: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


    【解决方案1】:

    我创建了一个助手。

      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)
    

    【讨论】:

      猜你喜欢
      • 2015-07-17
      • 2013-10-09
      • 2011-10-29
      • 2013-07-27
      • 2014-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多