Rails - form doesn't submit any data during AJAX call - file upload - 422 status code
<p>I have encountered a problem when trying to upload files with bootsy gem, but the problem is not related to bootsy itself. Bootsy generates a form with a following definition:</p>

<pre><code>&lt;form class="bootsy-upload-form form-inline" id="new_image" data-type="json" enctype="multipart/form-data" action="/bootsy/image_galleries/67/images" accept-charset="UTF-8" data-remote="true" method="post"&gt;
&lt;input name="utf8" value="✓" type="hidden"&gt;
&lt;input name="authenticity_token" id="authenticity_token" value="1pQMR5j33OKupMg4TSnQafLQx1BxzWjkP1KqTfZafqDRfGqwB8r2J4FzRE+dyuoHVOw/W0qd1FZ1JoJsJFThDQ==" type="hidden"&gt;
...
</code></pre>

<p>When I try to upload a file, this line is executed:</p>

<pre><code>this.uploadInput.closest('form').submit();
</code></pre>

<p>I have added alert before it so I would see how the serialized data of the form looks like and all the fields are shown as expected (including authenticity token etc.):</p>

<pre><code>alert(this.uploadInput.closest('form').serialize());
</code></pre>

<p>When the form is submitted, no data is send during POST request, only headers, nothing is seen in browser inspector, nothing can be seen in log files of rails, it just looks like this:</p>

<pre><code>Started POST "/bootsy/image_galleries/47/images" for ::1 at 2016-02-05 11:20:04 +0100
Processing by Bootsy::ImagesController#create as HTML
Parameters: {"image_gallery_id"=&gt;"47"}
Can't verify CSRF token authenticity
Completed 422 Unprocessable Entity in 0ms (ActiveRecord: 0.0ms)
FATAL -- : 
ActionController::InvalidAuthenticityToken - ActionController::InvalidAuthenticityToken:
_ actionpack (4.2.4) lib/action_controller/metal/request_forgery_protection.rb:181:in `handle_unverified_request'_
</code></pre>

<p>I have authenticity token generated in the form, I have the token in meta tag, everything looks fine, no error is thrown anywhere. I also tried to create sample app which is similar to my real project and it worked as expected, form normally submitted data - when I tried to compare HTML code and javascript events attached to both, they were almost similar expect a few parts because of other gems like ajax_pagination etc., but there were no parts, which should cause such a behavior.</p>

<p>I am using Rails 4.2.4, turbolinks are disabled using the attribute data-no-turbolink on body element, project uses bootstrap and contains JS libraries like jQuery, underscore, parsley, momentjs.</p>

<p>I would appreciate any thoughts, what could went wrong, why the form should not submit any data, where could be a problem. Thanks in advance for any tip.</p>

<p><strong>UPDATE:</strong>
Just to clarify things, I have taken a picture of a state before sending using AJAX - this javascript is part of jquery_ujs = RoR adapter for jQuery. You can see, that data contains all the form fields before send:
<a href="https://i.stack.imgur.com/ZezIk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZezIk.png" alt="enter image description here"></a></p>

<p>But data is NOT being send to the server:
<a href="https://i.stack.imgur.com/Pde56.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Pde56.png" alt="enter image description here"></a></p>

<p>On the other hand in my second working project, data is being sent:
<a href="https://i.stack.imgur.com/6Boaj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6Boaj.png" alt="enter image description here"></a></p>

<p><strong>UPDATE 2:</strong>
Just a few more information, bootsy gem, which is responsible for creation of the form uses remotipart to attach files to the request. Still...I was debugging the javascript and was unable to identify the problem. Both projects have the same version of jquery and remotipart, also the same version of rails. Looks like this will stay a mystery.</p>

<p><strong>UPDATE 3:</strong>
So I have almost resolved the issue - uploading is now working, it looks like it was a problem with order of javascript libraries. I'll post the result as soon as I pinpoint the exact issue - I'll reverse the changes and try to fix it again.</p>