[英]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.