Ruby-on-rails link_to :confirm 显示两次弹出窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4475449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:55:28  来源:igfitidea点击:

link_to :confirm displays popup twice

ruby-on-rails

提问by user370731

This tag with rails 3

这个标签与 rails 3

<%= link_to 'Destroy', item, :method => :delete,:confirm=>'Are you sure?' %>

produces this html

产生这个 html

<a href="/news/3" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Destroy</a>

The entry is deleted, the problem is that the popup appears twice.

条目被删除,问题是弹窗出现了两次。

What could be causing this?

什么可能导致这种情况?

回答by BMB

I was having this same issue, and spent the better part of an hour looking into it. I found a solution in my situation, and I figured I would share it in case it helps...

我遇到了同样的问题,花了一个小时的大部分时间来研究它。我在我的情况下找到了解决方案,我想我会分享它,以防它有帮助......

My problem was that I had

我的问题是我有

config.assets.debug = true 

in config/environments/development.rb

config/environments/development.rb

Changing that line to

将该行更改为

config.assets.debug = false

fixed the confirmation duplication for me. I found the relevant information in this rails guide to asset pipeline. Hope it helps!

为我修复了确认重复。我在这个 rails guide to asset pipeline 中找到了相关信息。希望能帮助到你!

回答by Scott

It sounds like the confirmation handler Javascript in rails.js is being attached twice.

听起来 rails.js 中的确认处理程序 Javascript 被附加了两次。

Could you be accidentally including two copies of rails.js via duplication of a javascript_include_tag helper?

您是否会通过复制 javascript_include_tag 助手意外地包含两个 rails.js 副本?

回答by Nockenfell

I've been used to include the javascript_include_tagat the bottom of my haml-layout-file.

我已经习惯将 包含javascript_include_tag在我的haml-layout-file 的底部。

With Rails4 and Turbolinks it happens that:

使用 Rails4 和 Turbolinks 会发生以下情况:

  • Everything ok on the first load of a page (popup for confirmation appears only once)
  • Visiting another page -> popup occurs twice
  • Visiting one more page -> popup occurs three times
  • and so on
  • until I reload the page.
  • 第一次加载页面时一切正常(用于确认的弹出窗口只出现一次)
  • 访问另一个页面 -> 弹出两次
  • 再访问​​一页 -> 弹出 3 次
  • 等等
  • 直到我重新加载页面。

I solved the problem by moving the javascript_include_tagfrom the bottom into <head>

我通过javascript_include_tag从底部移动到<head>

回答by Rk220

I'm using Rails 4 and none of the answers worked for me, however the following worked... I changed the line:

我正在使用 Rails 4,但没有一个答案对我有用,但是以下有效......我改变了这一行:

<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %> 

to

<%= javascript_include_tag 'application', 'data-turbolinks-eval' => false %>.

https://github.com/rails/turbolinks/issues/244

https://github.com/rails/turbolinks/issues/244

回答by Fernando Fabreti

Great answers here,

很棒的答案在这里,

But if you did everything and still get those double popups, and if you are running in development mode, check if you have public/assetsloaded with compiled assets.

但是如果你做了所有的事情仍然得到那些双弹出窗口,如果你在开发模式下运行,检查你是否public/assets加载了编译好的资产。

Deleting public/assets/*solves the issue.

删除即可public/assets/*解决问题。

回答by antonio vazquez

I copy this solution from another post, but this is what worked for me (rails 5)

我从另一篇文章中复制了这个解决方案,但这对我有用(rails 5)

"remove jquery_ujs from your application.js as rails_ujs is enough for later rails version."

“从 application.js 中删除 jquery_ujs,因为 rails_ujs 足以用于更高版本的 rails 版本。”

I had included: //= require jquery //= require jquery-ujs //= require rails-ujs

我已经包括: //= require jquery //= require jquery-ujs //= require rails-ujs

and after deleting it, all works fine. Solution from: Why am I getting a JQuery alert twice in Rails?

删除后,一切正常。解决方案来自:为什么我在 Rails 中两次收到 JQuery 警报?

回答by Nockenfell

In my case jQuery was loaded twice because of the line //= require_tree .

在我的情况下,由于该行,jQuery 被加载了两次 //= require_tree .

To prevent this error in application.js and application.css I'm used to create a subdirectory app/assets/javascript/autorequireand instead of require_tree .I do require_tree ./autorequire.

为了防止在application.js中这个错误,application.css我用来创建一个子目录app/assets/javascript/autorequire和,而不是require_tree .我做的require_tree ./autorequire

So, files in app/assets/javascriptand app/assets/stylesheetsare not included automatically/accidentally anymore. I put all my individual .css and .js files into the subdirectory and they are included implicitly. But I can define which files from the top-path are to be included and in which order.

因此,app/assets/javascriptapp/assets/stylesheets中的文件不再自动/意外地包含在内。我将所有单独的 .css 和 .js 文件放入子目录中,它们被隐式包含。但是我可以定义要包含顶级路径中的哪些文件以及以何种顺序包含在内。

Since I do so, I never had troubles by assets not loaded as I expect them to be.

自从我这样做以来,我从来没有因为资产没有按预期加载而遇到麻烦。

回答by Espen

This seem to be a bug in Rails. Apparently directives in application.js are not only expanded into individual files when debug mode is enabled, but they are also included in application.js. I haven't looked at the Rails code for this but assume it is due to application.js being the default JavaScript file. If you rename this file to something else, lets say default.js it will in debug mode correctly include the files specified by the directive and default.js will only output JavaScript which is only in that file. Thus not generating duplicate code.

这似乎是 Rails 中的一个错误。显然 application.js 中的指令不仅在启用调试模式时扩展为单个文件,而且它们也包含在 application.js 中。我没有为此查看 Rails 代码,但假设这是由于 application.js 是默认的 JavaScript 文件。如果您将此文件重命名为其他名称,假设 default.js 将在调试模式下正确包含指令指定的文件,而 default.js 将仅输出仅在该文件中的 JavaScript。因此不会生成重复的代码。

Example:

例子:

application.js

应用程序.js

//= require jquery_ujs

//= 需要 jquery_ujs

foo();

富();

Results in:

结果是:

1) jquery_ujs.js

1) jquery_ujs.js

2) application.js with jquery_ujs contents and foo()

2) 带有 jquery_ujs 内容和 foo() 的 application.js



default.js

默认.js

//= require jquery_ujs

//= 需要 jquery_ujs

foo();

富();

Results in:

结果是:

1) jquery_ujs.js

1) jquery_ujs.js

2) default.js with foo()

2) 带有 foo() 的 default.js

回答by Carsten

I've had the same problem with Rails 3.2.12, but simply updating to 3.2.13 solved that problem for me.

我在 Rails 3.2.12 上遇到了同样的问题,但只需更新到 3.2.13 就为我解决了这个问题。

回答by tmark

In my case (Rails 3.2.13), I had to delete rails.jsto fix the same problem.

就我而言(Rails 3.2.13),我不得不删除 rails.js来解决同样的问题。

I did not explicitly reference rails.js, nor did changing config.assets.debug help.

我没有明确引用 rails.js,也没有更改 config.assets.debug 帮助。