CarrierWave版本路径

时间:2016-09-13 07:47:04

标签: ruby-on-rails ruby ruby-on-rails-4 carrierwave

我有模特Catagory

class Category < ActiveRecord::Base
      mount_uploader :image, CategoryImageUploader
    end

我需要使用名称(id保存类别)保存图像

class CategoryImageUploader < CarrierWave::Uploader::Base

  include CarrierWave::RMagick

  storage :file


  mounted:
  def store_dir
    #"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end
  def store_path(for_file = filename)
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}/#{for_file}"
  end

  version :small do
    process resize_to_fit: [48, 48]
    def filename(uploaded_file = file)
      if uploaded_file.present?
        "small_#{model.id}.#{uploaded_file.extension}"
      end
    end
  end
  version :big do
    process resize_to_fit: [200, 200]
    def filename(uploaded_file = file)
      if uploaded_file.present?
        "big_#{model.id}.#{uploaded_file.extension}"
      end
    end
  end

  def filename(uploaded_file = file)
    if uploaded_file.present?
      "#{model.id}.#{uploaded_file.extension}"
    end
  end
  # Override the filename of the uploaded files:
  # Avoid using model.id or version_name here, see uploader/store.rb for details.
  # def filename
  #   "something.jpg" if original_filename
  # end
end

我使用https://coderwall.com/p/mulldw/custom-file-names-with-carrierwave-and-amazon-s3 来保存这样的图像 3.png 4.png

版本:小版本:大 aslo save(如big_3.png small_3.png)

但是

**Category.last.image_url =>  "/uploads/category/image/10/10.png" 
Category.last.image_url(:small) = >  "/uploads/category/image/10/10.png"
Category.last.image_url(:big) = >  "/uploads/category/image/10/10.png"**

所有链接指向一个图像10.png

我需要

  **Category.last.image_url =>  "/uploads/category/image/10/10.png" 
    Category.last.image_url(:small) = >  "/uploads/category/image/10/small_10.png"
    Category.last.image_url(:big) = >  "/uploads/category/image/10/big_10.png"**

0 个答案:

没有答案