Carrierwave giving error on console

578 views Asked by At

I installed carrierwave gem in my rails app.

My uploader class

class VoiceUploader < CarrierWave::Uploader::Base

  storage :file

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

end

My model class

require 'carrierwave/orm/activerecord'

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

But whenever I use store method in my rails console I get this error.

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!'

I also don't understand why model and mounted are nil even though I have defined mounter in my model class

1

There are 1 answers

2
Rostyslav Diachok On BEST ANSWER

You should store a file, not a string

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

Or try to use model for that

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