...">

Rails AJAX uploads form using Remotipart

I hit my head trying to get the AJAX download form. I am using Rails 3.2. I put the gem "remotipart", "~> 1.0" into my Gemfile, ran bundle install and successfully installed it.

Previously, I had this download form different from ajax, and I added :remote => true according to the literature on the github page:

 <%= form_tag "/administration/data_imports", :multipart => true, :remote => true %> <label for="file">Input File</label> <%= file_field_tag "file" %> <button type="submit">Import Data</button> </form> 

and this is in my data_imports_controller create action:

 def create file = params[:file] filename = file.original_filename end 

I added javascript include to my page for jquery.remotipart.js because it was important, although there was no explicit instruction for this.

I tried and it did not give a server error:

 Completed 500 Internal Server Error in 4ms NoMethodError (undefined method `original_filename' for nil:NilClass): app/controllers/data_imports_controller.rb:16:in `create' 

It’s clear that I’m doing something fundamentally wrong, but I need a hand.

+4
source share
5 answers

After a ton of debugging and reading through lots of code, I finally got the last stone (1.0.2) working with Rails 3.2.8. I was nailed by three cracks:

  • I disabled the file input field before submitting the form, and this causes remotipart to ignore it for inclusion in the iframe view. You must ensure that the file inputs are enabled. This is probably what you see, and why another file attribute selector works for you.
  • During my debugging, I redefined the jquery.iframe transport with the last upstream source and it does not have support for the hidden X-Http-Accepts variable in the iframe. You must use the version complete with the remotipart gem.
  • Remember to include a callback for ajax: fill out the form if you are using a data type other than 'script'. This is the default if you do not specify dataType in the global ajax parameters or use the data type attribute on the form.
+3
source

Lucky. I had the same problem a while ago. :) Add this to your application.js:

 //= require jquery.remotipart //= require jquery.iframe-transport 

You can get the iframe-transport file here: http://cmlenz.github.com/jquery-iframe-transport/ .

And as I said as a comment on another answer: I believe :multipart not required because you are using Rails 3.2. Not 100% sure, because I'm still using Rails 3.1 at work.;)

Hope this helps!

Edit

I created a sample application showing how to add remotipart to enable the loading of an AJAX file. This works great for me.

https://github.com/RobinBrouwer/remotipart_example

See commits inside this repository for steps that have been taken.

+1
source

I managed to achieve something in accordance with what I wanted, curbing in such a way that I really do not understand, but hey, it kind of works. I had to add jQuery line by line:

 $("#myform").submit(function(e) { e.preventDefault(); $.ajax(this.action, { files: $(":file", this), iframe: true }).complete( function(data) { eval(data.responseText); }); }); 

I believe that it takes the form of a presentation, initiates an ajax message with this iframe transport lizard, and then evaluates what is being returned (because in my case I am returning javascript). I also had to remove :remote => true from the form that he posted twice or something like that.

Thought I'd post it here so that he could help someone or get someone to do it better.

+1
source

In my case, I had an old version of jquery.iframe-transport.js

+1
source

The problem is how you actually declare yours :multipart=> true

Please use the code below and your form will then take a file parameter.

  form_for "/administration/data_imports",:remote => true,:html => {:multipart => true} 
0
source

All Articles