gitmyhub

carrierwave

Ruby ★ 8.8k updated 29d ago

Classier solution for file uploads for Rails, Sinatra and other Ruby web frameworks

Ruby library for handling file uploads in web applications, letting you attach user-uploaded photos or documents to database records and store them on local disk or cloud storage with minimal code.

RubyRuby on RailsRacksetup: easycomplexity 2/5

CarrierWave is a Ruby library that handles file uploads in web applications. It works with Ruby on Rails and other web frameworks built on top of Rack, the common Ruby web server interface. Developers add it to their project to manage how user-uploaded files, like profile photos or documents, are received, stored, and served back.

The library centers on a concept called an uploader, which is a class you define to describe the rules for a particular type of file. A generator command scaffolds the class for you, and from there you decide where files go (local disk or a cloud storage service), what transformations to apply (like resizing an image), and how filenames are handled. Once the uploader class is set up, you attach it to a database column in your model with a single line, and the library wires everything together so that assigning a file to the model attribute automatically handles saving and retrieving it.

Both single-file and multiple-file uploads are supported. For multiple files, you mount the uploader with a slightly different method name and store the list in a JSON or array column in the database.

Files can be stored on the local filesystem or on external services. Other options, such as Amazon S3, are available through additional configuration. You can also create multiple versions of an uploaded file, which is useful for generating thumbnails at different sizes from an original image.

The README covers upgrading between versions, validating file types and sizes, white-listing allowed extensions, and integration with image-processing libraries. The full README is longer than what was shown.

Where it fits