I am developing Rails 3.2.9 app and using Carrierwave as file uploader. The Carriverwave readme point out the way to get correct content_type:
- Add require 'carrierwave/processing/mime_types' to an initializer or your uploader(s).
- Add include CarrierWave::MimeTypes to your uploader.
- Add process :set_content_type to your uploader(s).
Base on this, My uploader is below:
# encoding: utf-8
require 'carrierwave/processing/mime_types'
class AttachmentUploader < CarrierWave::Uploader::Base
include CarrierWave::MimeTypes
storage :file
def store_dir
"#{base_store_dir}/#{model.id}"
end
process :set_content_type
end
In my model, mount the uploader as file:
mount_uploader :file, AttachmentUploader
However, I always got content_type nil after upload file:
1.9.3-p327 :013 > a.file.class
=> AttachmentUploader
1.9.3-p327 :010 > a.file.file
=> #<CarrierWave::SanitizedFile:0x00000004046330 @file="uploads/course/000/000/026/attachment_file/6/myIcon.png", @original_filename=nil, @content_type=nil>
Any suggestion? Thanks.
PS: I already added gem "mime-types", "~> 1.19"
in my Gemfile
.