4

I've been trying to upload an image using ajax, but maybe I'm doing it wrong

<%= simple_form_for @note, remote: true do |f|%>
  <%= f.text_area :content%>
  <%= f.file_field :picture %>
  <%= f.submit "save"%>
<%end%>

but every time I hit save... it is not doing the upload, it goes to the show action instead of displaying the alerts that I have in the file create.js.erb (it does have the multipart parameter in the form label. Is it possible to upload an image using this way? or should I check the jquery-file-upload library?

Thanks in advance

Javier

JavierQQ23
  • 704
  • 1
  • 8
  • 20

2 Answers2

4

You can not use Ajax for a file upload. But you can use e.g. the fileupload plugin for jQuery http://blueimp.github.com/jQuery-File-Upload/

I had the same problem --> form_tag with remote: true does not make an ajax request

Community
  • 1
  • 1
Bjoernsen
  • 2,016
  • 1
  • 33
  • 41
  • well that makes sense :), I've seen that plugin. What should I search in google in order to understand why it is not possible? – JavierQQ23 Sep 14 '12 at 06:38
  • 1
    this helped too http://stackoverflow.com/questions/7595049/why-cant-i-upload-files-asynchronously – JavierQQ23 Sep 14 '12 at 06:43
  • in my projects with a file upload, I use the jQuery file upload. As soon as a file is selected, it is uploaded to my server and a response is send to the user. If the file passes the tests, it is a html including a hidden id. If the file fails, I send an error message to the user. – Bjoernsen Sep 14 '12 at 06:43
2

You can use jquery.form plugin for file upload through ajax. Here is a link for that.

http://jquery.malsup.com/form/

Samiron
  • 5,169
  • 2
  • 28
  • 55
Muhammad Sannan Khalid
  • 3,127
  • 1
  • 22
  • 36