Sunday, 10 February 2019

jQuery conflict on Rails

My application.js:

//= require rails-ujs
//= require jquery
//= require activestorage
//= require turbolinks
//= require bootstrap-sprockets
//= require flatpickr.min
//= require_tree .

My Gemfile:

gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.2'
gem 'turbolinks', '~> 5'
gem 'bootstrap-sass'
gem 'jquery-rails'

If I I just leave my app like that, all my jQuery will work fine. But I also need

<head>
  <script src="https://code.jquery.com/jquery-3.3.1.min.js"</script>
</head>

on my application.html.erb since I am using Bootstrap 4.

So with this configuration, I am experiencing lot of issues with my jQuery, like

$(...).flatpickr is not a function

Is there a way to avoid those conflicts? I suppose that's because I'm including multiple jQueries on my app.

If I remove //= require jquery all the Bootstrap-related jQuery will work, but my custom scripts won't.
If I remove <script src="https://code.jquery.com/jquery-3.3.1.min.js"</script> my scripts will work but all Bootstrap-related won't (like the hamburger navbar or similars).



from jQuery conflict on Rails

No comments:

Post a Comment