简体   繁体   中英

Rails escape_javascript and remotipart

When replacing html with jquery and rails (tested on 4.0.4) the following works:

Content of replace.js.erb

$('#my_div').html("<%= j(render 'my_partial') %>");

Content of _my_partial.html.erb

"This" works
<%= '"This" also works' %>

However it fails when replace.js.erb is rendered in reponse to a ajax file submit using the remotipart gem (1.2.1) as shown below.

Content of create.html.erb

<%= form_for @my_model, :html => {:remote => true} do |f| %>
  <%= f.file_field :some_file %>
  <%= f.submit %>
<% end %>

It causes the following javascript error in the browser because quotes aren't escaped correctly.

SyntaxError: Unexpected identifier

Server response: $('#my_div').html("\\"This\\" works\\n"This" fails\\n");

Note that if I submit without selecting a file the javascript error does not occur and it re-renders correctly. It looks like remotipart isn't invoked when a file isn't selected, probably because a multipart form isn't required.

我终于解决了同样的问题:

$("#my_div").html("<%= escape_javascript(render :partial => 'my_partial').gsub('&quot;', "'") %>");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM