Rails Paperclip and AJAX form

我只是一个虾纸丫 提交于 2019-12-11 11:26:53

问题


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


回答1:


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:

  • HTML5: http://www.queness.com/post/11434/7-javascript-ajax-file-upload-plugins
  • Flash: http://code.google.com/p/swfupload/
  • iframe method: http://www.google.com/search?q=ajax%2Biframe%2Bupload

I hope this help




回答2:


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



来源:https://stackoverflow.com/questions/13625686/rails-paperclip-and-ajax-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!