RAIRS 3 UJS-控制器通过link_to两次调用:远程

发布于 2024-10-03 03:20:07 字数 1138 浏览 6 评论 0原文

我有一个奇怪的问题,即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">
  <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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

愛放△進行李 2024-10-10 03:20:07

感谢这个对话框:

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 头部动态添加脚本标签时,它还为应该是空的动态 application.js 添加了一个,除了加载来自 public/assets 的静态 application.js 。令人困惑?是的!

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!

魂ガ小子 2024-10-10 03:20:07

我遇到了同样的问题,通过

\#config.assets.debug = true

在开发模式下评论这一行来解决

I had the same issue, fixed by commenting this line

\#config.assets.debug = true

in development mode

放手` 2024-10-10 03:20:07

我有类似的问题。运行以下 rake 任务解决了我的问题。

rake tmp:clear
rake assets:clean

I had simmilar issue. Running following rake tasks resolved my problem.

rake tmp:clear
rake assets:clean
盛装女皇 2024-10-10 03:20:07

我认为发生的情况是您的 JavaScript 没有阻止浏览器执行链接。因此,您会收到 AJAX 请求,然后收到浏览器请求。

Rails Cast 174 中有对此的解释和解决方案。文本解释此处,大约向下翻页 3/4。

I think what is happening is that your JavaScript is not stopping the link from being executed by the browser. So you get the AJAX request then the browser request.

There is an explanation and solution for this in Rails Cast 174. Text explanation here, about 3/4 of the page down.

删除→记忆 2024-10-10 03:20:07

我遇到了同样的问题:使用 :remote => 时出现重复请求真的。我的问题是由于使用 Heroku 和预编译资产造成的。我在部署到 Heroku 之前预编译资产(rake asset:precompile)。这实际上会在您的公共文件夹中创建资产管道的重复副本。因此,正如 Docuext 提到的那样,发生了同样的事情:有多个 javascript 文件处理每个请求。

如果我在本地工作时删除公共目录中的资产(git rm -rf public/assets),它就会得到修复。从公共目录中删除资产后,它停止发送多个请求

I was having the same issue: duplicate requests when using :remote => true. My issue was due to using Heroku and precompiling assets. I am precompiling assets prior to deploying to Heroku (rake assets:precompile). This essentially creates a duplicate copy of your asset pipeline in your public folder. So the same thing was happening as mentioned by Docunext: there were multiple javascript files handling each request.

It gets fixed if I remove the assets in the public directory when working locally (git rm -rf public/assets). After removing the assets from the public directory, it stopped sending multiple requests

无戏配角 2024-10-10 03:20:07

尝试此代码...以防止双击远程链接..

$('a[data-remote=true]').live('ajax:before', function () {
   if ($(this).attr('ajax-loading')) {
     return false;
   } else {
     $(this).attr('ajax-loading', true);
   }
}).live('ajax:complete', function () {
  $(this).removeAttr('ajax-loading');
});

Gist

Try this code.... to prevent double clicks on remote links..

$('a[data-remote=true]').live('ajax:before', function () {
   if ($(this).attr('ajax-loading')) {
     return false;
   } else {
     $(this).attr('ajax-loading', true);
   }
}).live('ajax:complete', function () {
  $(this).removeAttr('ajax-loading');
});

Gist

江挽川 2024-10-10 03:20:07

对我来说也有同样的问题:两次 XMLHttpRequest
link_to ... :remote => “true”

我已经解决了,在

locals/application.rb 中:

通过注释

// config.assets.paths << Rails.root.join('vendor','assets','stylesheets','javascripts')

并取消注释

config.action_view.javascript_expansions[:defaults] = %w(jquery jquery-ui application)

然后我忘了在上次 git pull 后做什么。
我希望能帮助某人。

Also for me the same problem: twice XMLHttpRequest on a
link_to ... :remote => "true"

I've solved, in

locals/application.rb :

by commenting

// config.assets.paths << Rails.root.join('vendor','assets','stylesheets','javascripts')

and uncomment

config.action_view.javascript_expansions[:defaults] = %w(jquery jquery-ui application)

then I forgot to do after my last git pull.
I hope help someone.

擦肩而过的背影 2024-10-10 03:20:07

您想要作为 :defaults 的 JavaScript 文件(始终包含 application.js)。

 config.action_view.javascript_expansions[:defaults] = %w(jquery rails)

注释行清楚地表明 application.js 始终包含在内。
在我的应用程序中,我已默认添加应用程序。因此,对于 ajax .submit 它在我的应用程序中创建了两条记录。

JavaScript files you want as :defaults (application.js is always included).

 config.action_view.javascript_expansions[:defaults] = %w(jquery rails)

Commented line is clearly saying that application.js is always included.
In my application I have added application in defaults. So for ajax .submit it was creating two records in my application.

桃扇骨 2024-10-10 03:20:07

我刚才也遇到了同样的问题。检查了所有代码更改并尝试了其他方法但无济于事。最后清除浏览器缓存。这有帮助。

i faced the same problem just now. checked out all the code changes and tried other things but to no avail. finally cleared the browser cache. that helped.

草莓味的萝莉 2024-10-10 03:20:07

如果您无法找到两次加载 ujs 文件的位置,只需将此代码复制到rails.js / application.js 中(最好不要在这里)

var alreadyInitialized = function() {
    var events = $(document).data('events');
    return events.click && $.grep(events.click, function(e) { return e.namespace === 'rails'; }).length;
    }

if ( alreadyInitialized() ) {
    $.error('jquery-ujs has already been loaded!');
}

If you are not able to find out where the ujs file is loaded twice, simply copy this code into rails.js / application.js (preferably not here)

var alreadyInitialized = function() {
    var events = $(document).data('events');
    return events.click && $.grep(events.click, function(e) { return e.namespace === 'rails'; }).length;
    }

if ( alreadyInitialized() ) {
    $.error('jquery-ujs has already been loaded!');
}
记忆里有你的影子 2024-10-10 03:20:07

根据 Eugene 的回答,请注意,如果您在本地预编译用于生产的资产后进行开发,例如...

bundle exec rake assets:precompile RAILS_ENV=production

...那么当您继续开发时,Rails AssetPipeline 将希望同时服务于两者您的未编译您的已编译资源。

解决这个问题的一种方法是使用 Eugene 的 Rails 的标准 rake 任务。另一种方法是简单地删除那些讨厌的预编译生产就绪资产,例如

$ rm -rf #{Rails.root}/public/assets

Building on Eugene's answer, note that if you're developing after you've locally precompiled your assets for production, like...

bundle exec rake assets:precompile RAILS_ENV=production

...then as you continue development the Rails AssetPipeline will want to serve both your uncompiled and your compiled assets.

One way to fix it is with Eugene's Rails' standard rake task. Another way is by simply removing those pesky precompiled production-ready assets with something like

$ rm -rf #{Rails.root}/public/assets
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文