JangoSteve/remotipart

JS response is not executed when response status is 422

Closed this issue · 1 comments

Hello,

I have a remotipart form which executes the response when the controller responds with a 200, and does not when it responds with a 422. Is it expected? Is there any way to handle 422 responses with remotipart?

Thank you

I found the issue: the problem was not the response status code but the fact that the response contains a render partial: ..., and remotipart wraps response into a <textarea data-type="text/javascript"></textarea>, so its content should be HTML escaped. I resolved turning the create.js.erb view from this:

<%- rendering = j render(partial: 'form_errors') %>
$('#form_errors').replaceWith('<%= rendering %>');

into this:

<%# .to_str turns an  ActiveSupport::SafeBuffer instance into a String,
  which gets HTML escaped %>
<%- rendering = j render(partial: 'form_errors').to_str %>
$('#form_errors').replaceWith('<%= rendering %>');

Were you aware of it? I close this issue and open another one, at least in order to document this behaviour.