1

I have been trying to upload images through remotipart and carrierwave. Simply fileupload is working fine, I have also installed remotipart gem which enables file upload through ajax.Now the issue is how to send the file through ajax(I mean the jquery part).

This is how I'm trying to send the file to my controller,but its not working

$("#upload").live("submit",function(e) {

    e.preventDefault();
    var report ={};
    report.file =$("#new_upload").find('#upload_name').val(); 

    $.ajax(this.action, {
        data:  report,
        iframe: true,
        processData: false
    }).complete(function(data) {
        console.log(data);
    });
}); 

Here's my form code:

<%= form_for(@upload,:url => { :action => "save_remotipart" },:html => {:multipart => true },:remote => (params[:action] ==  true )) do |f| %>

  <fieldset>
    <legend class='required'>
      Required fields
    </legend>
      <div class="field">
        <%= f.label :name %><br />
        <%= f.file_field :name %>
      </div>
    </fieldset>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

When the form is submitted No parameters are sent to the server. On abort I'm getting this:

{"object Object"=>nil}

Please help me.

Sachin Prasad
  • 5,365
  • 12
  • 54
  • 101

2 Answers2

2

You don't have to do $("#upload").live(){}, adding :remote => true to the form will do an Ajax submit with file-uploads done properly.

I am using it in one of my projects (see form code below)

<%= simple_form_for @post, :remote => true, :multipart => true do |f| %>

            <%= f.input :content, :label => false, :input_html => { :rows => '2', :id => "ibtb" } %>


        <%= f.input :file, :label => false %>

        <%= f.button :submit, "Post" %>
<% end %>

I am using simple_form gem, but it should work for a normal form as well since both these generate HTML at the end. The example in github page for gem also doesn't use simple_form.

Hope this helps.

  • What if I have more than one file with same name but different data and other input datas also? – Sachin Prasad Feb 18 '13 at 12:17
  • Do you want to provide ability to upload multiple files in the same form ? – Raghavan Kandala Feb 19 '13 at 04:48
  • 1
    Yeah .. with same name and those input will be generated dynamically. – Sachin Prasad Feb 19 '13 at 19:10
  • hi @RaghavanKandala, have you a problem of a message that never stops showin in the bottom of the navigator (firefox) which is "Transferring data from [localhost]" when you upload file via ajax – medBouzid Nov 06 '13 at 20:43
  • my simple_form was not working with remotipart, not sure why, but if I make my own button and listener (i.e. not a :submit button), and manually submit the form in javascript, it works using remotipart: ```:javascript $(document).ready(function(){ $("#ajax-create-public-image-form-btn").click(function(e){ $form = $('#ajax-create-public-image-form'); $form.submit(); }); });``` – ryan2johnson9 Sep 03 '19 at 06:56
0

I had the same issue. Follow this link it might be helpfull. http://www.javaroots.com/2015/04/asynchronous-file-uploads-in-rails.html#disqus_thread

Santosh Mohanty
  • 468
  • 1
  • 6
  • 23