Rails remote multipart form with remotipart bind ajax:success

Rails 3.x / 4.0 does not handle remote multipart forms natively, so a workaround is needed. To get remote multipart forms working correctly you can use the following steps (quick solution).

First: Add the remotipart gem to your GemFile

gem 'remotipart', '~> 1.2.1'

Then, Update your javascript manifest file (in basic configurations this is usually found in assets/javascript/application.js). Include the following requirement:

//= require jquery.remotipart

In my case, I’m binding the form via jquery and expecting a JSON response. To ensure you actually receive a JSON response, add the following to your <%= form_for ... %> method:

:html=>{:'data-type' => :json,:multipart=>true}

Obviously, there is going to be more to your form_for method, but this is minimally what your :html option should look like.

Now you can bind on ajax:success in the normal way:

$(document).on("ajax:success","#your-form-element",function(e, data, status, xhr) { ... });

I’m writing this post primarily because my multipart form was processing however I couldn’t bind to ajax:success.

On a side note, you can achieve pretty much the same effect by using the ajaxForm jquery plugin. However, I really just wanted a quick solution that hooked into my current development flow, so I went this way.

1 thought on “Rails remote multipart form with remotipart bind ajax:success

Leave a Reply

Your email address will not be published. Required fields are marked *