使用Rails上传AJAX文件

I have a form, with an upload field, that is displayed in a modal box and when I submit it, the errors are shown without closing this modal, but when an error occurs and there is a file already uploaded in the file field the request is processed in HTML instead of JavaScript. Does anyone know what can I do to solve this issue? Thanks

application_controller.rb

skip_before_action :verify_authenticity_token, if: :json_request? #add this line to json request

your_action.html.erb

<%= form_for @object, :html => {:multipart => true}, remote: true do |f| %>
    <%= f.file_field :file%>
    <%= f.submit "Save" %>
<% end %>

your_object_controller

@object = Object.new(object_params)

respond_to do |format|
    if @object.save
        format.js
    end
end

your_action.js.erb

<% if remotipart_submitted? %>
    alert('submitted via remotipart');
<% else %>
    alert('submitted via native jquery-ujs');
<% end %>

for more documentation, visit https://github.com/JangoSteve/remotipart