javascript - 如何从 rails 中的 js.erb 传回 jquery 字符串
问题描述
我在 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 中评论我想添加的任何注释,而通常你可以用/*...*/
我正在考虑remote: true
从我正在使用的表单中删除,并在提交和设置为时进行正常的 ajax 调用,dataType
但html
我仍然认为在此导轨已经损坏我的字符串之前。
解决方案
做一个部分:- _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 - 动态表中的动态按钮句柄单击
- sql - 如何使用 SQL 获取列中每个分区的第一个和最后一个值
- r - 计算数据框中组内出现的比率
- javascript - webgl纹理格式,类型和internalFormat的无效组合
- javascript - 在将对象的 getter 下划线发送到 API 之前对其进行规范化
- bash - bash,测试curl是否存在,如果不存在则安装
- c++ - 如何消除并行 std::transform_reduce() 的中间容器?
- javascript - 当输入数量未知时如何从 Material UI 输入中获取值
- php - PHP 中的嵌套 API 调用
- django - 此字段是必需的。Dajngo 休息框架