I am using rails 5, with remotipart. The remotipart version is:
gem 'remotipart', github: 'mshibuya/remotipart'
(At the moment of this question, the current commit is 88d9a7d55bde66acb6cf3a3c6036a5a1fc991d5e).
When I want to submit a form having multipart: true and
remote: true` but without sending any attached file, it works great. But when I send a file, it fails.
To illustrate the case, consider a response like this:
(function() {
modelErrors('<div class=\'alert alert-danger alert-dismissible\' role=\'alert\'>\n <div aria-label=\'Close\' class=\'close fade in\' data-dismiss=\'alert\'>\n <span aria-hidden>\n ×\n <\/span>\n <\/div>\n Code can\'t be blank\n<\/div>\n');
}).call(this);
When this response is executed, immediately after arriving (since it is js), the form looks as it expected (in this case, this validation error is right to happen, and the rendered danger alert is right to appear with such text):
However, when I fill the file field, and repeat the exact same case (exact same validation error), the form looks quite different:
If you can guess, the contents are passed as text. The actual response being received from the server is a bit different:
<script type="text/javascript">try{window.parent.document;}catch(err){document.domain=document.domain;}</script>(function() {
modelErrors('<div class=\'alert alert-danger alert-dismissible\' role=\'alert\'>\n <div aria-label=\'Close\' class=\'close fade in\' data-dismiss=\'alert\'>\n <span aria-hidden>\n ×\n <\/span>\n <\/div>\n Code can\'t be blank\n<\/div>\n');
}).call(this);
This is the actual body of my response (it is not a bad copypaste, but the actual response as wrapped by remotipart).
The modalErrors
function is quite dumb:
function modalErrors(html) {
$('#main-modal > .modal-dialog > .modal-content > .modal-body > .errors').html(html);
}
Comparing the jQuery-appended html chunks (I look for them in the browser's DOM inspector), they look like this:
Good:
<div class="alert alert-danger" role="alert">
<ul style="list-style-type: none">
<li>Code can't be blank</li>
</ul>
</div>
Bad:
Code can't be blank
What am I missing here? What I want is to allow my resposes to append html content when needed.