简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM