weixin_33743661 2012-11-29 12:18 采纳率: 0%
浏览 45

Rails回形针和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.

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条回答 默认 最新

  • python小菜 2012-11-29 13:03
    关注

    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

    评论

报告相同问题?