Posted over 2 years ago. Visible to the public. Repeats.

FileIO: Writing strings as Carrierwave uploads

When you have string contents (e.g. a generated binary stream, or data from a remote source) that you want to store as a file using Carrierwave, here is a simple solution.

While you could write your string to a file and pass that file to Carrierwave, why even bother? You already have your string (or stream).
However, a plain StringIO object will not work for Carrierwave's ActiveRecord integration:

>> Attachment.create!(file: TypeError: no implicit conversion of nil into String

This is because Carrierwave expects a filename by default. It makes sense, so here is a simple yet useful helper class:

class FileIO < StringIO def initialize(stream, filename) super(stream) @original_filename = filename end attr_reader :original_filename end

You must simply come up with a filename and can then pass your string or stream directly.

>> Attachment.create!(file:, 'document.pdf')) => #<Attachment ...>

Once an application no longer requires constant development, it needs periodic maintenance for stable and secure operation. makandra offers monthly maintenance contracts that let you focus on your business while we make sure the lights stay on.

Owner of this card:

Arne Hartherz
Last edit:
over 2 years ago
by Arne Hartherz
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Arne Hartherz to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more