Rails Carrierwave實作設定

取得存檔資訊
u = User.new
u.avatar = params[:file]
u.avatar = File.open('somewhere')
u.save!
u.avatar.url # => '/url/to/file.png'
u.avatar.current_path # => 'path/to/file.png'
u.avatar.identifier # => 'file.png' 
 
class MyUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick
  storage :file 


  process :resize_to_fit => [800, 800]

  version :thumb do
    process :resize_to_fill => [200,200]
  end
  def store_dir
    'public/my/upload/directory'
  end
  def cache_dir
    '/tmp/projectname-cache'
  end
  def default_url
    "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  end

end



uploader = AvatarUploader.new
uploader.store!(my_file)                              # size: 1024x768

uploader.url # => '/url/to/my_file.png'               # size: 800x600
uploader.thumb.url # => '/url/to/thumb_my_file.png'   # size: 200x200 

留言

熱門文章