Carrierwave在控制台上给出错误

时间:2015-06-11 19:25:58

标签: ruby-on-rails ruby file-upload carrierwave

我在rails应用程序中安装了carrierwave gem。

我的上传课程

class VoiceUploader < CarrierWave::Uploader::Base

  storage :file

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

end

我的模特课

require 'carrierwave/orm/activerecord'

class PulseFeedback < ActiveRecord::Base
    belongs_to :tablet
    mount_uploader :voice, VoiceUploader
end

但每当我在rails控制台中使用store方法时,我都会收到此错误。

2.2.0 :001 > v = VoiceUploader.new
=> #<VoiceUploader:0x000000038dc8b0 @model=nil, @mounted_as=nil>

2.2.0 :002 > v.store!("/home/raghu/Music/ajeeb.mp3")
    CarrierWave::FormNotMultipart: CarrierWave::FormNotMultipart
        from /home/raghu/.rvm/gems/ruby-2.2.0/gems/carrierwave-0.10.0/lib/carrierwave/uploader/cache.rb:120:in `cache!'
        from /home/raghu/.rvm/gems/ruby-2.2.0/gems/carrierwave-0.10.0/lib/carrierwave/uploader/store.rb:56:in `store!'

我也不明白为什么模型和装载都是零,即使我在我的模型类中定义了贴片机

1 个答案:

答案 0 :(得分:1)

您应该存储文件,而不是字符串

class VoiceUploader < CarrierWave::Uploader::Base

  storage :file

  def store_dir
    "uploads"
  end

end

v = VoiceUploader.new

File.open("/home/raghu/Music/ajeeb.mp3") do |f|
  v.store!(f)
end

或尝试使用模型

PulseFeedback.create({
  voice: File.open("/home/raghu/Music/ajeeb.mp3")
})