本文介绍了Rails的回形针和AJAX形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含形式,用户可上传照片的模式。一旦照片被正确上传我想通过Ajax响应并显示一条消息。

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.

不幸的是,形式subbmitted在HTML中,而不是在JS。

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

你知道吗?

表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 %>

感谢

推荐答案

您无法上传AJAX的文件。这可能是为什么提交的原因是在HTML中,而不是在JS。

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

我希望这有助于

这篇关于Rails的回形针和AJAX形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 21:15