In my previous article we learned how we to enable file upload inRails using Refile. Today we will look at another file uploading gem – Shrine . Shrine is a another toolkit for file uploads in Ruby applications.

In its introductory blog post , the author of Shrine, Janko Marohnić, indicates that Shrine is heavily influenced by both Refile and CarrierWave. Shrine makes use of a plugin system for everything, shipping with tons of plugins , some of which I will make use of today.

We will be using Shrine to build a Rails application that stores images and books. Yeah books…you might want to show your friends your latest collection of books. I hope you love to read as much as I do. Let’s build this thing and put it out for the world to see.

ImageMagick Installation

For Shrine to work you need ImageMagick installed. Depending on your operating system, use one of the steps below.

Mac Users:

brew install imagemagick

Ubuntu Users:

sudo apt-get install imagemagick

If neither of the above works for you, meaning you are on Windows or something else, check the installations page on the ImageMagick site. There are binary releases for Windows 7 and above.

FileSystem

FileSystem storage deals with uploads to the file system of the application itself. It gets initialized with a base folder and a prefix , like we have above. The prefix is a directory relative to the base folder where files will be stored, and it gets included in the URL.

S3

S3 storage is responsible for uploads to the Amazon Web Service (AWS) S3 service. To use S3 storage, you will need the aws-sdk gem in your Gemfile . Do not forget to bundle install .

Now go over to config/initilazers/shrine.rb to set things up for S3 storage.

At first we require shrine and shrine s3 . S3 prefix option is passed the name of the bucket where our files will be uploaded. In the example we have above, there will be two (2) buckets; one for store , the other for cache . The next option, s3-options , gives us access to AWS. You will need to get your AWS access key and secret access key and put them into the environment variables.

I want us to separate the error messages, that is a new habit I have recently developed. Create a folder in your views named application . Create a partial calledj _error_messages.html.erb and paste in the error messages:

Versions

In your Rails application, you may want to store a thumbnails (or other versions) alongside the original image. For that you need to load the versions plugin, which I mentioned above. We did that already in the image_uploader.rb file. Now, just have the #process method return a Hash of versions:

More from this author

Conclusion

In this article we learned how to enable image uploading in our Rails application using Shrine. You can checkout Shrine’s page on Github if you want to try out some exciting features not mentioned here. Shrine has some great support for putting file uploads into background jobs or direct uploads, just to name a couple of features we didn’t cover today. Also, Shrine is not just for Rails, you can use it with any Ruby application.