Cloudinary Blog

Attachinary - a modern attachments solution for Ruby on Rails

By Nadav Soferman
When developing a website you are required for a somewhat tedious work of handling dynamically uploaded content. The constantly added content includes images uploaded by your users and content administrator, user documents and other files.
 
As a developer, you'll be responsible for adding integration of attachments to your application's model and taking care of the upload, normalization, storage, transformation and delivery of such assets.
 
Over time, we've run into a lot of attachment management libraries for many of the web development frameworks available. For Ruby on Rails alone there are CarrierWave, Paperclip, Dragonfly, attachment_fu and quite a few others. 
 
While Cloudinary streamlines all your image management needs and takes care of uploads, storage, transformations, manipulations and delivery, you still need to integrate it with your application's model. To simplify the integration, Cloudinary offers many client libraries for all major web dev platforms and programming languages. For example, Cloudinary's Ruby GEM includes a plugin that seamlessly integrate with CarrierWave for managing uploads and image transformations in the cloud. We've covered this in a previous blog post.
 
Today, we wanted to tell you about a new attachment management library for Ruby on Rails - Attachinary. Attachinary was developed by Milovan Zogovic and it does an amazing job in simplifying attachment management in your application's model. 
 
We've tried Attachinary, really liked it and wanted to share our experience working with it. 
 

How is Attachinary different? 

There are so many other attachment management libraries, why do we need another one? 
Well, here are a few reasons:
 
  • Attachinary offers non-intrusive integration with ActiveRecord and Mongoid model. You no longer need to maintain DB columns in your model entities for referencing uploaded images.
  • With attachinary it's very easy to switch between a single image and multiple images for a model entity.
  • Attachinary allows for simple integration through Rails 3.2's modern Asset Pipeline.
  • Attachinary uses Cloudinary for uploading files to the cloud, transforming images and delivering files through a fast CDN. No need to install any image manipulation software.
  • Attachinary has built-in integration with Cloudinary's jQuery-based direct upload from the browser. Progress indication and image preview included.
  • Attachinary is extremely simple to use and requires minimal changes to your existing code base.
 

How is Attachinary used?

After a very quick Cloudinary & Attachinary GEM installation and setup, you can add any attachment attribute to your model class. The following code shows a User model entity:
Copy to clipboard
class User < ActiveRecord::Base
  attr_accessible :name
  
  has_attachment  :avatar
  has_attachments :photos, :maximum => 3
end
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 very 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.
Copy to clipboard
= form_for(@user) do |user_form|
    = user_form.text_field(:name)
    = attachinary_file_field_tag 'user[avatar]', @user, :avatar
    = attachinary_file_field_tag 'user[photos]', @user, :photos
    
    = user_form.submit("Save")
Model management in your controller is exactly the same as always:
Copy to clipboard
def create
  @user = User.new(params[:user])
  @user.save!
end
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.
Copy to clipboard
- if user.avatar.present?
  = cl_image_tag(user.avatar.path, :width => 80, :height => 100,
                 :crop => :thumb, :gravity => :face)        
  - user.photos.each do |photo|
    = cl_image_tag(photo.path, :size => '70x50', :crop => :fill, :radius => 20)
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.
 
Last but not least - Attachinary also supports non-image raw files.
 

Summary 

We hope that with this quick introduction, we've managed to pique your interest about Attachinary. Together with Cloudinary, Attachinary is really easy to use and also very powerful. Attaching images to your Rails model has never been easier. 
 
For more details, setup instructions and usage examples, check out Attachinary's page at GitHub.
 
If you are a Rails developer, you should definitely give it a try.
 

Recent Blog Posts

Generate Waveform Images from Audio with Cloudinary

This is a reposting of an article written by David Walsh. Check out his blog HERE!
I've been working a lot with visualizations lately, which is a far cry from your normal webpage element interaction coding; you need advanced geometry knowledge, render and performance knowledge, and much more. It's been a great learning experience but it can be challenging and isn't always an interest of all web developers. That's why we use apps and services specializing in complex tasks like Cloudinary: we need it done quickly and by a tool written by an expert.

Read more
Make All Images on Your Website Responsive in 3 Easy Steps

Images are crucial to website performance, but most still don't implement responsive images. It’s not just about fitting an image on the screen, but also making the the image size relatively rational to the device. The srcset and sizes options, which are your best hit are hard to implement. Cloudinary provides an easier way, which we will discuss in this article.

Read more

The Future of Audio and Video on the Web

By Prosper Otemuyiwa
The Future of Audio and Video on the Web

Web sites and platforms are becoming increasingly media-rich. Today, approximately 62 percent of internet traffic is made up of images, with audio and video constituting a growing percentage of the bytes.

Read more

Embed Images in Email Campaigns at Scale

By Sourav Kundu
Embed Images in Email Campaigns at Scale

tl;dr

Cloudinary is a powerful image hosting solution for email marketing campaigns of any size. With features such as advanced image optimization and on-the-fly image transformation, backed by a global CDN, Cloudinary provides the base for a seamless user experience in your email campaigns leading to increased conversion and performance.

Read more
Build the Back-End For Your Own Instagram-style App with Cloudinary

Github Repo

Managing media files (processing, storage and manipulation) is one of the biggest challenges we encounter as practical developers. These challenges include:

A great service called Cloudinary can help us overcome many of these challenges. Together with Cloudinary, let's work on solutions to these challenges and hopefully have a simpler mental model towards media management.

Read more

Build A Miniflix in 10 Minutes

By Prosper Otemuyiwa
Build A Miniflix in 10 Minutes

Developers are constantly faced with challenges of building complex products every single day. And there are constraints on the time needed to build out the features of these products.

Engineering and Product managers want to beat deadlines for projects daily. CEOs want to roll out new products as fast as possible. Entrepreneurs need their MVPs like yesterday. With this in mind, what should developers do?

Read more