Jercoh
Jercoh

Reputation: 103

Rails Paperclip and AJAX form

I have a modal containing a form, in which the user can upload a photo. Once the photo is correctly uploaded I want to respond via Ajax and display a message.

Unfortunately the form is subbmitted in HTML and Not in JS.

Any idea?

Form code:

<%= form_for(Object.new() , :url => {:controller => 'objects', :action => 'create'}, :remote=> true, :html => { :multipart => true }) do |f| %>
     <%= f.file_field :photo, label: 'add a photo' %>
     <%= button_tag(type: 'submit', class: "btn btn-success") do %>
            <i class="icon-arrow-up icon-white upload"></i>Upload
     <% end %>
<% end %>

Thanks

Upvotes: 1

Views: 2515

Answers (2)

CupraR_On_Rails
CupraR_On_Rails

Reputation: 2489

You can't upload a file with AJAX. It's probably the reason why the submit is in HTML and not in JS.

Nevertheless you have some hack to solve this problem:

I hope this help

Upvotes: 2

RoryB
RoryB

Reputation: 1047

Unfortunately rails cannot upload files directly with ajax, as javascript does not have direct access to to hard drive files. Apparently there's a workaround involving iframes: this previous question may help you:

RAILS - paperclip don't work with Ajax

and a bit on file uploading in rails forms:

http://guides.rubyonrails.org/form_helpers.html#dealing-with-ajax

Upvotes: 1

Related Questions