ruby-on-railsruby-on-rails-3carrierwaveremotipart

ajax fileupload through rails remotipart gem


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.


Solution

  • 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.