Skip to content

How to: Upload from a string in Rails 3

clyfe edited this page May 31, 2011 · 12 revisions

Rails 3 no longer provides a StringIO#original_filename method, so uploading files from a string doesn't work. For example, the following worked in Rails 2:

StringIO.new("foobar").original_filename # => "stringio.txt"

But in Rails 3, we see this error:

StringIO.new("foobar").original_filename NoMethodError: undefined method original_filename for StringIO

The quick and dirty solution is to add the following in an initializer:

StringIO.class_eval { def original_filename; "stringio.txt"; end }
Clone this wiki locally