例如,我为该帖子提供了许多评论。
<%= render post.comments %>
在部分评论中,我有javascript,它会重复很多次我有多少评论
_comment.html.erb
<div id="content_<%= comment.id %>"><%= comment.content %></div>
<script>$("content_<%= comment.id %>").click(function() { alert('content of comment <%= comment.id %>') });</script>
避免在源代码中重复 javascript 的最佳方法是什么?我试图创建 *_comment.js.erb* 文件并将 javascript 放在那里,但我不明白如何使它工作。
谁能解释一下如何解决这个问题?