2017-05-15 78 views
0

什麼是在您的本地磁盤上開發圖像文件和在生產中使用Cloudinary的優雅解決方案?我有Paperclip,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

回答

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)