【发布时间】:2012-07-07 18:21:27
【问题描述】:
例如,我为帖子渲染了许多 cmets。
<%= render post.comments %>
在部分 cmets 中,我有 javascript,它会重复很多次,我有多少 cmets
_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 放在那里,但我不明白如何使它工作。
谁能解释一下如何解决这个问题?
【问题讨论】:
标签: javascript ruby-on-rails ajax