Rails 3.2 Ajax Paperclip Image uploading

The other day, I was working on uploading images using Paperclip Gem. I wanted to use Ajax to do this. I searched online a lot, but every result said that it is not possible. Every page result was saying that you can’t upload images using Ajax. Some Jquery plug-ins were available, but it was not behaving like what I really needed on my project. Finally I got a simple solution for this problem.

First of all download the the js code and save it as jquey.form.js in assets/javascripts. Include it in application.js as
//= require jquery.form

Next is your view. It may look like this

Next is your form to upload image

[You can use bootstrap modal to render this form. Then it’ll be more beautiful]

Then add some Javascript to the file where the form is rendered

Then write the method in corresponding controller

Note: You can use this method for submitting forms which handle fields of any data types. So if you have more fields, you can simply add those in the form and view file. Ajax will remain same.

Hope this tip helps on your respective projects 🙂

Jyothish Kumar is a Consultant Developer at Compassites Software Solutions Private Limited. His areas of work includes Ruby On Rails and Web technologies. Prior to Compassites, he worked at Mobme Wireless Solutions Private Limited. He is an active contributor on websites that offer technical help. Jyothish also has a technology blog of his own. He loves working on Ruby on Rails related projects. He hails from God’s own country (Kerala).

Leave a comment