Rails: File upload via ajax using remotipart

11 Dec 2014

Rails remote is one of the best way to submit a form with ajax. But it is not capable of uploading files using ajax. That’s where remotipart gem comes into action.

Add gem to Gemfile

# Gemfile
gem 'remotipart'

After adding remotipart to Gemfile do the bundle install.

Add to javascript manifest

Then you need to require remotipart in javascript manifest.

// app/assets/javascripts/application.js

//= require jquery.remotipart 

set multipart option for form

As in every form with fileupload, you need to set the multipart option.

<%= form_for(@upload, remote: true, multipart: true) do |f| %>
    <%= f.file_field :image %>
    <%= f.submit %>
<% end %>

Thats it, you don’t need any extra configuration for the forms. A example application is available on github.

Further references

If you find my work useful, I appreciate donations via PayPal.
comments powered by Disqus