小编cha*_*dru的帖子

Rails 3 UJS - 控制器被link_to:remote调用两次

我有一个奇怪的问题,JQuery正在为link_to方法创建两个AJAX请求.我正在使用JQuery for UJS开发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>
Run Code Online (Sandbox Code Playgroud)

我的控制器设置如下:

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
Run Code Online (Sandbox Code Playgroud)

最终将js呈现为:

 $('#follow_link').html('<%= escape_javascript(render :partial => "follow") %>');
Run Code Online (Sandbox Code Playgroud)

这基本上用相同的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>
Run Code Online (Sandbox Code Playgroud)

然而,这在某种程度上导致JQuery发出两个AJAX请求.

任何人都可以看到这里有什么问题吗?

我正在使用'jquery-rails'gem,它正在将最新的jquery-ujs文件复制到我的应用程序中.JQuery版本是1.4.3

jquery ujs ruby-on-rails-3

33
推荐指数
4
解决办法
1万
查看次数

标签 统计

jquery ×1

ruby-on-rails-3 ×1

ujs ×1