link_to :confirm displays popup twice

2020-01-27 02:49发布

This tag with rails 3

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

produces this 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?

16条回答
一纸荒年 Trace。
2楼-- · 2020-01-27 03:32

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

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

I had included: //= 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?

查看更多
做个烂人
3楼-- · 2020-01-27 03:34

In the layout haml like this have the twice pop up problem

%head
  = stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload'
  = javascript_include_tag 'application', 'data-turbolinks-track': 'reload'
%body
  #calendar.calendarsize

Then I commend it and it works.

%head
  = stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload'
  -#= javascript_include_tag 'application', 'data-turbolinks-track': 'reload'
%body
  #calendar.calendarsize
查看更多
▲ chillily
4楼-- · 2020-01-27 03:37

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

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

So, files in app/assets/javascript and app/assets/stylesheets are 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.

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

查看更多
Fickle 薄情
5楼-- · 2020-01-27 03:37

just remove the turbolinks, that worked for me in rails4

查看更多
Bombasti
6楼-- · 2020-01-27 03:38

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.

Example:

application.js

//= require jquery_ujs

foo();

Results in:

1) jquery_ujs.js

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


default.js

//= require jquery_ujs

foo();

Results in:

1) jquery_ujs.js

2) default.js with foo()

查看更多
疯言疯语
7楼-- · 2020-01-27 03:39

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

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

查看更多
登录 后发表回答