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

查看:100
本文介绍了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>

并且我的控制器已设置为:

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") %>');

基本上替换了'< 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

解决方案

感谢此对话框:

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

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

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

似乎出于某种原因,application.js会自动将app/assets/javascripts/中的所有内容捆绑在一起-即使我删除了 all 所有要求.

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

希望这会有所帮助!

更新:我认为这可能与我在不使用摘要的情况下预渲染资产有关,所以当我的开发环境从app/assets/application中的require动态地在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天全站免登陆