代码之家  ›  专栏  ›  技术社区  ›  Lee Eather

如何从rails中的js.erb返回jquery字符串

  •  0
  • Lee Eather  · 技术社区  · 7 年前

    我在一个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中评论任何我想在其中添加的注释,通常情况下,您可以使用 /*...*/

    我想把 远程:真 从我正在使用的表单,并在提交和设置时执行普通的Ajax调用 dataType 作为 html 但我仍然认为在这个轨道之前,已经损坏了我的字符串。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Anand    7 年前

    做一个部分: _示例\u 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%>");