javascript - 如何从 Rails 中的 js.erb 传回 jquery 字符串

标签 javascript jquery ruby-on-rails

我在 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/

相关文章:

javascript - 两个 Ajax onclick 事件

javascript - jQuery - 从具有相同类的元素中获取不同的值

javascript - 防止后退按钮上的 hashchange

javascript - reddit 是如何在弹出登录表单时让整个背景变黑的?

javascript - METEOR - 将数据从父模板传递到子模板

javascript - jQuery $(document).off() 在 Chrome 扩展中不起作用

php - 通过电子邮件发送在 HTML5 canvas 上创建的图像

mysql - 在 rails 中检查数据库中的重复记录

ruby-on-rails - 如何在Rails 3中使用带有has_many关联的fields_for分配值

ruby-on-rails - 在 Ruby/Rails 中使用带有 restful_authentication 的 ssl_requirement 时遇到问题