简体   繁体   中英

Paperclip, Cloudinary, Development, Production

What is an elegant solution to having image files on your local disk in development and on Cloudinary in production? 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.

%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.

References:

https://github.com/thoughtbot/paperclip
http://www.rubydoc.info/gems/paperclip/Paperclip
https://github.com/GoGoCarl/paperclip-cloudinary
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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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