Carrierwave没有设置content_type

时间:2013-04-12 03:05:53

标签: ruby-on-rails carrierwave plupload

我正在使用carrierwave上传图片。我遇到的问题是content_type没有保存到数据库中。我可以看到content_type被传递到请求中(如下所示),但它似乎没有进入数据库。

upload.rb

class Upload < ActiveRecord::Base
  mount_uploader :file, FileUploader
  attr_accessible :file
end

file_uploader.rb

require 'carrierwave/processing/mime_types'

class FileUploader < CarrierWave::Uploader::Base
  include CarrierWave::MimeTypes

  storage :fog
  process :set_content_type
end

uploads_controller.rb

  def create
    @upload = Upload.new params[:upload]
    if @upload.save
        render :json => @upload
    end
  end

schema.rb

  create_table "uploads", :force => true do |t|
    t.date     "expired_at"
    t.datetime "created_at",        :null => false
    t.datetime "updated_at",        :null => false
    t.string   "file"
    t.text     "slug"
    t.string   "file_content_type"
  end

POST请求

Started POST "/uploads" for 127.0.0.1 at 2013-04-11 22:54:56 -0400
Processing by UploadsController#create as */*
  Parameters: {"name"=>"IMG_0004.JPG", "authenticity_token"=>"00kuSJQvgkWmCo+bWBQQzhzwC2qWdsF6T3K6DLuv0l8=", "upload"=>{"file"=>#<ActionDispatch::Http::UploadedFile:0x007fcb34399570 @original_filename="IMG_0004.JPG", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"upload[file]\"; filename=\"IMG_0004.JPG\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/var/folders/9f/zccg30y961g1kl0zc_kzkfw00000gn/T/RackMultipart20130411-90625-1je1uyu>>}}

2 个答案:

答案 0 :(得分:0)

将您的列名更改为:content_type,它应该开始工作。

答案 1 :(得分:0)

如果在我的模型文件中尝试了同样的问题,我安装了上传器

  before_save :set_mime_type                   

    def set_mime_type
      self.mimetype = Mime::Type.lookup_by_extension(File.extname(self.cf_filename.to_s)[1..-1]) 
    end

注意:您需要在表格中有一个mimetype字段