views:

190

answers:

1

I have simple model which looks like this:

def video_file=(input_data)
  unless input_data.to_s.empty?
    newfile = File.open("#{RAILS_ROOT}/public/to_upload/#{self.filename}_vid.f4v", "wb") do |f|
      while buff = input_data.read(4096)
        f.write(buff)
      end
    end
  end
end

and here the error which rails manages to display and then dies, literally.

 ActiveRecord::StatementInvalid in <ControllerName>

Why?

+2  A: 

Replace

newfile = File.open(path, "wb") do |f|
while buff = input_data.read(4096)
  f.write(buff)
end

with

if input_data.respond_to?(:read)
  File.open(path, "wb") { |f| f.write(input_data.read) }
end
Can Berk Güder