1

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

2 Answers 2

2

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

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.