link_to :confirm displays popup twice

Ruby on-Rails

Ruby on-Rails Problem Overview


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?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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

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!

Solution 2 - Ruby on-Rails

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

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

Solution 3 - Ruby on-Rails

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

With Rails4 and Turbolinks it happens that:

  • 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.

I solved the problem by moving the javascript_include_tag from the bottom into <head>

Solution 4 - Ruby on-Rails

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

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

to

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

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

Solution 5 - Ruby on-Rails

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: https://stackoverflow.com/questions/48113810/why-am-i-getting-a-jquery-alert-twice-in-rails

Solution 6 - Ruby on-Rails

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/assets loaded with compiled assets.

Deleting public/assets/* solves the issue.

Solution 7 - Ruby on-Rails

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()

Solution 8 - Ruby on-Rails

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

Solution 9 - Ruby on-Rails

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.

Solution 10 - Ruby on-Rails

So for me it was because i had defined data-remote instead of just remote.

IE

data: { remote: true, ... }

instead of

remote: true, data: { ... }

hope that helps.

Solution 11 - Ruby on-Rails

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.

Solution 12 - Ruby on-Rails

In my case, it was that the application had both "mootools ujs" and "jquery ujs" included. I got rid of the mootools one and now I only see one confirmation.

Solution 13 - Ruby on-Rails

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

Solution 14 - Ruby on-Rails

just remove the turbolinks, that worked for me in rails4

Solution 15 - Ruby on-Rails

Try to run either

rake assets:precompile ENV=development 

or

rake assets:precompile ENV=production

Solution 16 - Ruby on-Rails

Delete:

//= require jquery
//= require jquery_ujs
//= require_tree .

from app/assets/javascripts/application.js. Despite of been commented it loads these js files. It works for me.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser370731View Question on Stackoverflow
Solution 1 - Ruby on-RailsBMBView Answer on Stackoverflow
Solution 2 - Ruby on-RailsScottView Answer on Stackoverflow
Solution 3 - Ruby on-RailsNockenfellView Answer on Stackoverflow
Solution 4 - Ruby on-RailsRk220View Answer on Stackoverflow
Solution 5 - Ruby on-Railsantonio vazquezView Answer on Stackoverflow
Solution 6 - Ruby on-RailsFernando FabretiView Answer on Stackoverflow
Solution 7 - Ruby on-RailsEspenView Answer on Stackoverflow
Solution 8 - Ruby on-RailsCarstenView Answer on Stackoverflow
Solution 9 - Ruby on-RailsNockenfellView Answer on Stackoverflow
Solution 10 - Ruby on-Railsuser20611View Answer on Stackoverflow
Solution 11 - Ruby on-RailstmarkView Answer on Stackoverflow
Solution 12 - Ruby on-RailsMikey HogarthView Answer on Stackoverflow
Solution 13 - Ruby on-RailsYing LyuView Answer on Stackoverflow
Solution 14 - Ruby on-RailsRailsZilla.comView Answer on Stackoverflow
Solution 15 - Ruby on-RailsJerlan LorestoView Answer on Stackoverflow
Solution 16 - Ruby on-RailsRamon MartinezView Answer on Stackoverflow