我有一个奇怪的问题,JQuery 正在为 link_to 方法创建两个 AJAX 请求。我正在为 UJS 开发一个带有 JQuery 的 Rails 3 应用程序。我有一个切换链接,可以在“关注”和“取消关注”之间切换
我的链接呈现如下:
<span id="follow_link">
<a href="/tfollow_artist?id=8103103" data-method="post" data-remote="true" id="follow_artist" rel="nofollow">Unfollow</a>
</span>
我的控制器是这样设置的:
def tfollow_artist
@artist = Artist.find(params[:id])
if current_user.following?(@artist)
current_user.stop_following(@artist)
else
current_user.follow(@artist)
end
end
最终将 js 呈现为:
$('#follow_link').html('<%= escape_javascript(render :partial => "follow") %>');
它本质上用相同的 URL 替换了 '<span id="follow_link">...</span> 的 html 内容,只是文本不同。例如,上面现在将呈现为:
<span id="follow_link">
<a href="/tfollow_artist?id=8103103" data-method="post" data-remote="true" id="follow_artist" rel="nofollow">Follow</a>
</span>
然而,这以某种方式导致 JQuery 发出两个 AJAX 请求。
任何人都可以看到这里有什么问题吗?
我正在使用“jquery-rails”gem,它将最新的 jquery-ujs 文件复制到我的应用程序中。jQuery 版本是 1.4.3