简体   繁体   English

Rails:JS Controller 由于某种原因被调用了两次

[英]Rails: JS Controller Being Called Twice for Some Reason

For some reason when I click on a button, my controller and the resulting jquery function gets called twice.由于某种原因,当我单击一个按钮时,我的 controller 和生成的 jquery function 被调用了两次。 Since the js function being called is toggle, this is a problem as it causes the code to jump in and out of view.由于调用的 js function 是切换的,因此这是一个问题,因为它会导致代码跳入和跳出视图。

Here is the form:这是表格:

Unseen Notifications: <%=  current_user.notification_unseen %> </div>
                <%= button_to "show", {:action => "seen", :controller=>"notifications"}, :remote => true %>

Here is the controller:这是 controller:

def seen
    respond_to do |format|
      format.js 
    end
  end

Here is the jquery:这是 jquery:

$("div#notifications").toggle();
$("div#count").html("<%= escape_javascript( render :partial => 'notifications/count')%>");

I am at a loss for why on earth this would possibly happen.我不知道为什么这可能会发生。 I have been very careful not to double click and don't know how to prevent it.我一直非常小心不要双击,也不知道如何防止它。 Any suggestions would be greatly appreicated.任何建议将不胜感激。

I had a recent problem like this before, when I had 2 jquery libraries included.我最近遇到过这样的问题,当时我有 2 个 jquery 库。 In particular, check that you only have jquery_ujs.js and not jquery.js.特别是,检查您是否只有 jquery_ujs.js 而不是 jquery.js。 Seems like when you include both of them certain js functions are called twice.似乎当你同时包含它们时,某些 js 函数会被调用两次。

Hope this helps!希望这可以帮助!

If Benjamin's suggestion doesn't work, you could go with cheats and hacks and add 1 to some global var on each click (which would happen twice), then use modulus to only trigger your action when (global_counter % 2 == 0).如果本杰明的建议不起作用,您可以使用作弊和黑客攻击 go 并在每次单击时为某个全局变量加 1(这会发生两次),然后使用模数仅在 (global_counter % 2 == 0) 时触发您的操作。

Just to be clear, this is a terrible solution that you should avoid using if at all possible...需要明确的是,这是一个糟糕的解决方案,您应该尽可能避免使用......

I'm new to rails and had the same problem.我是 Rails 新手,遇到了同样的问题。 It turns out I had:原来我有:

<%= javascript_include_tag "application" %>

In both my view and in application.html.erb.在我看来和在 application.html.erb 中。 I removed one of them and voila, fixed.我删除了其中一个,瞧,修复了。

If you're running jquery in rails 5.1 and beyond, as rails dropped jquery support and switch to its own ujs, you need to remove //= require rails-ujs from application.js file.如果您在 rails 5.1 及更高版本中运行 jquery,因为 rails 放弃了 jquery 支持并切换到自己的 ujs,您需要从 application.js 文件中删除 //= require rails-ujs。

I had the same problem while running latest version of rails, this solved my problems.我在运行最新版本的 rails 时遇到了同样的问题,这解决了我的问题。

Similar to Michael Mayer 's and Benjamin Tan Wei Hao 's answers:类似于Michael MayerBenjamin Tan Wei Ha的回答:

it is possible for a second <%= javascript_pack_tag 'application' %> to get added to the layout (even automatically) when implementing Webpacker.在实现 Webpacker 时,第二个<%= javascript_pack_tag 'application' %>可能会被添加到布局中(甚至是自动添加)。 This happened recently, when modernizing an older application that isn't quite ready for Rails 7.这发生在最近,当现代化一个还没有为 Rails 7 做好准备的旧应用程序时。

TL;DR; TL;博士;

Beware the second application.js reference.当心第二个application.js参考。

Taking the jquery_ujs.js worked for me.服用 jquery_ujs.js 对我有用。

Take a close look at the file application.html.erb to see if that is referring to jquery.js and jquery_ujs.js.仔细查看文件application.html.erb ,看看它是否指的是 jquery.js 和 jquery_ujs.js。 If so, just take the jquery_ujs.js out, refresh the browser and try it again.如果是这样,只需将 jquery_ujs.js 取出,刷新浏览器并重试。

Also, take a look at the page source and see if jquery_ujs and jquery is referred to at the same time.另外,看一下页面源码,看看是否同时引用了jquery_ujs和jquery。 If so, search in the source files for references to that jquery_ujs.如果是这样,请在源文件中搜索对该 jquery_ujs 的引用。

Worked for me, thanks guys.为我工作,谢谢大家。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM