Rails 3 UJS - 控制器被 link_to 调用两次:remote [英] Rails 3 UJS - controller gets called twice by link_to :remote

查看:20
本文介绍了Rails 3 UJS - 控制器被 link_to 调用两次:remote的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题,其中 JQuery 正在为 link_to 方法创建两个 AJAX 请求.我正在为 UJS 开发带有 JQuery 的 Rails 3 应用程序.我有一个在关注"和取消关注"之间切换的切换链接

我的链接呈现如下:

<a href="/tfollow_artist?id=8103103" data-method="post" data-remote="true" id="follow_artist" rel="nofollow">取消关注</a></span>

我的控制器设置如下:

def tfollow_artist@artist = Artist.find(params[:id])如果 current_user.following?(@artist)current_user.stop_following(@artist)别的current_user.follow(@artist)结尾结尾

最终将 js 渲染为:

 $('#follow_link').html('<%= escape_javascript(render :partial => "follow") %>');

它本质上替换了 '...</span> 的 html 内容.使用相同的 URL 只是文本不同.例如,上面现在将呈现为:

<a href="/tfollow_artist?id=8103103" data-method="post" data-remote="true" id="follow_artist" rel="nofollow">关注</a></span>

然而,这不知何故导致 JQuery 发出两个 AJAX 请求.

有人能看出这里有什么问题吗?

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

解决方案

感谢这个对话框:

https://github.com/rails/jquery-ujs/issues/208

我发现 jquery 和 jquery_ujs 被包含了两次.

我猜 jquery-rails gem 会自动将它们放入 application.js,然后我也将它们包含在 application.js 中.

似乎无论出于何种原因,application.js 都会自动捆绑 app/assets/javascripts/中的所有内容 - 即使我删除了所有需求.

因此,如果您的 :remote => 真实表单被提交两次,请尝试检查 application.js.

希望这有帮助!

更新:我相信这可能与我在不使用摘要的情况下预渲染我的资产有关,所以当我的开发环境从 app/assets/application.js 中的要求动态地在 html 头中添加脚本标签时.js,它还为应该是空的动态 application.js 添加了一个,除了来自 public/assets 的静态应用被加载.令人困惑?没错!

I have a weird problem where JQuery is creating two AJAX requests for a link_to method. I am developing a Rails 3 app with JQuery for UJS. I have a toggle link which toggles between 'Follow' and 'Unfollow'

My link is rendered as below:

<span id="follow_link">
  <a href="/tfollow_artist?id=8103103" data-method="post" data-remote="true" id="follow_artist" rel="nofollow">Unfollow</a>
</span>

and my controller is setup so:

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

which finally renders a js as:

 $('#follow_link').html('<%= escape_javascript(render :partial => "follow") %>');

Which essentially replaces html contents of the '<span id="follow_link">...</span> with the same URL only with the text being different. For example, above will now be rendered as:

<span id="follow_link">
  <a href="/tfollow_artist?id=8103103" data-method="post" data-remote="true" id="follow_artist" rel="nofollow">Follow</a>
</span>

However this is somehow causing JQuery to make two AJAX requests.

Can any one see what is wrong here?

I am using 'jquery-rails' gem which is copying the latest jquery-ujs file to my app. JQuery version is 1.4.3

解决方案

Thanks to this dialog:

https://github.com/rails/jquery-ujs/issues/208

I was able to discover that jquery and jquery_ujs were getting included twice.

I guess the jquery-rails gem automatically puts them into application.js, and then I had them included in application.js as well.

Seems like for whatever reason application.js automatically bundles everything in app/assets/javascripts/ - even when I remove all the requires.

So, if your :remote => true forms are getting submitted twice, try checking application.js.

Hope this helps!

UPDATE: I believe this might have had something to do with me pre-rendering my assets without using a digest, so then when my development environment adds script tags in the html head dynamically from the requires in app/assets/application.js, it also adds one for what should be an empty dynamic application.js, except the static one from public/assets gets loaded. Confusing? Yep!

这篇关于Rails 3 UJS - 控制器被 link_to 调用两次:remote的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆