link_to :confirm displays popup twice

后端 未结 16 1850
感情败类
感情败类 2020-11-30 02:38

This tag with rails 3

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

produces this html

相关标签:
16条回答
  • 2020-11-30 03:17

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

    0 讨论(0)
  • 2020-11-30 03:18

    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>

    0 讨论(0)
  • 2020-11-30 03:19

    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!

    0 讨论(0)
  • 2020-11-30 03:19

    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?

    0 讨论(0)
  • 2020-11-30 03:21

    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.

    0 讨论(0)
  • 2020-11-30 03:22

    Try to run either

    rake assets:precompile ENV=development 
    

    or

    rake assets:precompile ENV=production
    
    0 讨论(0)
提交回复
热议问题