2

I'm having a problem loading gems into my Rails 4 app.

I use jQuery. In my application.js, I have:

//= require jquery
//= require bootstrap-sprockets
//= require jquery_ujs
//= require html.sortable
//= require disqus_rails
//= require moment
//= require bootstrap-datetimepicker
//= require pickers
//= require main
//= require hammer.min
//= require jquery.animate-enhanced.min
//= require jquery.countTo
//= require jquery.easing.1.3
//= require jquery.fitvids
//= require jquery.magnific-popup.min
//= require jquery.parallax-1.1.3
//= require jquery.properload
//= require jquery.shuffle.modernizr.min
//= require jquery.sudoSlider.min
//= require jquery.superslides.min
//= require masonry.pkgd.min
//= require rotaterator
//= require smoothscrolljs
//= require waypoints.min
//= require wow.min
//= require gmaps/google
//= require chosen-jquery
//= require cocoon
//= require imagesloaded.pkgd.min
//= require isotope.pkgd.min
//= require jquery.counterup.min
//= require jquery.pjax
//= require custom.js
//= require slider
//= require dependent-fields
//= require bootstrap-slider

$.noConflict();
jQuery( document ).ready(function( $ ) {
  // Code that uses jQuery's $ can follow here.
    $(document).ready(function() {
        DependentFields.bind()
    });
});


//= require_tree .

In my gem file, I am trying to use rails dependent fields gem. That gem also requires underscore.js.

I think the reason this gem isn't working is because it uses javascript.

I'm having the same problem with the disqus rails gem (which is raising errors that are localhost/:94 Uncaught TypeError: $ is not a function

Does anyone know how to resolve conflicts between rails jQuery and javascript?

2 Answers 2

2

The problem is that you have set jQuery to work in noConflict mode through this line: $.noConflict();. jQuery will not define $ variable globally and will be available through jQuery variable only.

$.noConflict();   // <=====  HERE  ======
jQuery( document ).ready(function( $ ) {
  // Code that uses jQuery's $ can follow here.
    $(document).ready(function() {
        DependentFields.bind()
    });
});

The "plugins" that are raising this error are probably expecting $ variable to be set. They should not be doing that however.

You have 2 options:

  1. Remove $.noConflict(); line from your application.js file.
  2. Wrap the code that is raising this error in to a function where $ is set to jQuery variable.

Like this:

(function($){
  // Here: $ === jQuery
  // Put js code that raises error here
})(jQuery);

Also, you are using 2 document ready events in your code. You can improve it to this:

$.noConflict();
jQuery( document ).ready(function( $ ) {
  // Code that uses jQuery's $ can follow here.
  DependentFields.bind()
});
Sign up to request clarification or add additional context in comments.

8 Comments

I see. You need to go with the first option and remove $.noConflict(); line. Otherwise, find the gem on GitHub and create a pull request with the improvement.
I don't understand. Do I wrap the gem in the gem file in the code you suggest as option 1?
No, forget about everything and just delete this line from your application.js: $.noConflict();.
From what you provided, you don't have a conflict. The error message is Uncaught TypeError: $ is not a function, which means that a function with the name $ is not defined. If you remove line $.noConflict(); from your application.js file, it will create a function with that name.
but that's just one of the .js files in my app/assets/javascripts folder
|
0

Try to wrap your javascript code where you use $ variable with anonymous function:

(function($){

// put your code here

})(jQuery);

1 Comment

When I try that in my underscore.js file, I still can't get the dependent fields rails part working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.