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, transformations 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.
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:
class User < ActiveRecord::Base
has_attachments :photos, :maximum => 3
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.
Model management in your controller is exactly the same as always:
@user = User.new(params[:user])
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 80×100 face detection based thumbnail of the user’s avatar and a 70×50 rounded corner version of all uploaded photos.
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.
One of the main optimization challenges for website and mobile developers is how to display sufficiently high quality images to their visitors while minimizing the image file size. A smaller image file size can lead to faster load times, reduced bandwidth costs and an improved user experience. The problem…