我在 create.js.erb 文件中包含此代码,我希望从我使用 remote: true
提交的评论表单中返回该代码
<div class="comment">
<%= @comment.content %>
<%= link_to "delete", comment_path(@comment, :deletecomment =>
"delete"), remote: true, method: :delete, data: { confirm: "You
sure?" }
%>
</div>
看来rails只处理此文件中的有效jquery。我只能用 html 周围的 ''
来制作我的代码,使其成为 jquery 字符串,然后像这样将其发送回来,但有趣的是,检查返回的数据时的结果是一个带有单引号的字符串,如下所示字符串的一部分。
然后我使用 html = data.replace(/\'/g, "");
删除单引号,但是有更好的方法吗?
例如,我无法在 js.erb 中注释任何我想在其中添加的注释,而通常您可以使用 /*...*/
注释掉 js.erb
我正在考虑从我正在使用的表单中删除remote: true
,并在提交时执行正常的ajax调用并将dataType
设置为html
但我仍然认为在此之前 Rails 已经损坏了我的字符串。
最佳答案
制作部分:- _example_partial.html.erb
<div class="comment">
<%= comment.content %>
<%= link_to "delete", comment_path(comment, :deletecomment => "delete"),remote: true, method: :delete, data: { confirm: "You sure?" } %>
</div>
和create.js.erb
$("#id_of_div_where_the_partial_will_append").append("<%= j render 'example_partial', comment: @comment%>");
关于javascript - 如何从 Rails 中的 js.erb 传回 jquery 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50479416/