S
S
Shaks2016-05-07 02:51:16
JavaScript
Shaks, 2016-05-07 02:51:16

How to do ajax file upload in Rails 4?

We have the following form:

<%= form_for @model,  :remote => true, :html => {:multipart => true} do |f| %>
    <%= f.text_field :title, :length => 255, :class=>'character-counter', :required=>true %>
...........
    <%= f.file_field :thumb %>
    <%= f.button :type=>'submit' %>
<%-end %>

The form works as expected, files are uploaded to the server (using the remotipart gem) via the iFrame method. But, if the response is not 200, but something like 422 (for example, validation errors), then the selected files in the input are reset, and you have to re-select files for uploading to the server, which, in fact, is not acceptable.
How to solve the problem? (possibly not using remotipart)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question