Programmable Media

Attachinary integration

Last updated: Jul-19-2024

Attachinary is an attachment management library for Ruby on Rails that can help to simplify attachment management in your application's model, including:

  • Non-intrusive integration with ActiveRecord and Mongoid model. You no longer need to maintain DB columns in your model entities for referencing uploaded images.
  • Easy to switch between a single image and multiple images for a model entity.
  • Simple integration through Rails modern Asset Pipeline.
  • Uses Cloudinary for uploading files to the cloud, transforming images and delivering files through a fast CDN.
  • Built-in integration with Cloudinary's jQuery-based direct upload from the browser, including progress indication and image preview.

After installing and setting up the Attachinary gem, you can add any attachment attribute to your model class. The following code shows a User model entity:

The has_attachment and has_attachments methods were used to define a User that can have a single 'avatar' and up to 3 'photo' attachments. That's it, no additional migrations or code changes are required.

Uploading assets to Cloudinary is also quite simple with Attachinary. The following HAML view code shows how to create an upload form. The attachinary_file_field_tag view helper method is responsible for all the magic. When this form is submitted, all images will be automatically uploaded to the cloud directly from your website visitor's browser. Multiple photos can be uploaded from this same form and the identifiers of the uploaded images will be automatically stored in your model.

Model management in your controller is the same as always:

You can display uploaded images, generate thumbnails and apply image transformations by using Cloudinary's cl_image_tag. Here's an example of a view code that displays a 80x100 face detection based thumbnail of the user's avatar and a 70x50 rounded corner version of all uploaded photos:

Using Attachinary allows you to dynamically apply any image transformation supported by Cloudinary. All transformed images are generated on the fly in the cloud and are then cached and delivered through a fast CDN.

Cloudinary Academy


Check out our Introduction to Cloudinary for Ruby Developers course in the Cloudinary Academy. This self-paced resource provides video-based lessons, sample scripts and other learning material to get you going with Ruby and Cloudinary today.

✔️ Feedback sent!

Rate this page: