ifad / data-confirm-modal

Makes Rails' link_to confirm: 'foo' build a Bootstrap Modal instead of calling the browser's confirm() API.
MIT License
271 stars 115 forks source link

Remove use of `$.rails` to avoid silent initialization errors #61

Closed dcecile closed 6 years ago

dcecile commented 6 years ago

(Continued from https://github.com/twbs/bootstrap-rubygem/issues/127...)

In integrating data-confirm-modal into my Rails app, I accidentally used this ordering in my application.js file:

//= require rails-ujs
//= require turbolinks
//= require jquery3
//= require popper
//= require bootstrap
//= require_tree .

With this ordering data-confirm-modal doesn't perform Rails initialization, because $.rails wasn't set. I resolved the issue by moving the //= require jquery3 to the top. With jQuery included first, data-confirm-modal initialized without any problems.

I had originally created this as a documentation issue for bootstrap-rubygem (which explains the application.js setup), but @glebm has suggested that this be resolved by changing data-confirm-modal to stop using $.rails.

What do you think? Would an alternative to $.rails help make the initial setup of data-confirm-modal easier?

glebm commented 6 years ago

To clarify, in rails-ujs this is no longer $.rails but window.Rails.

You can't rely on $.rails because it will be undefined if rails-ujs is included before jquery, which is possible because rails-ujs does not depend on jQuery.

One option is to use (window.Rails || $.rails) instead.

vjt commented 6 years ago

Hi, sorry for the delay. I've pushed the sensible, suggested change, that should fix this issue. If all is well I can cut a new release.

dcecile commented 6 years ago

I've tried out the fix and it looks good to me.

Before the fix (with rails-ujs before jquery3), data-confirm-modal failed to load, resulting in native modal popups. After the fix, data-confirm-modal loaded fine, and I get Bootstrap modal popups. :+1:

vjt commented 6 years ago

Released v1.6.2 on RubyGems and npm.

Thanks for all the feedback!