link_to :confirm displays popup twice
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?
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!
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?
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>
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
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?
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.
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()
I've had the same problem with Rails 3.2.12, but simply updating to 3.2.13 solved that problem for me.
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.
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.
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.
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.
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
just remove the turbolinks, that worked for me in rails4
Try to run either
rake assets:precompile ENV=development
or
rake assets:precompile ENV=production
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.
精彩评论