select2-rails gem is not working on Rails4

前端 未结 4 560
一个人的身影
一个人的身影 2021-01-02 17:55

I have tried given documentation for the select2-rails gem, but my browser console still throws an error.

Uncaught TypeError: $(...).select2 is not a

相关标签:
4条回答
  • 2021-01-02 18:32

    On rails 5 using CoffeeScript and turbolinks I had to use the line:
    $(document).on "turbolinks:load", -> before everything so that the code gets executed when the desired page with the select tag is loaded.

    0 讨论(0)
  • 2021-01-02 18:38

    I had the same problem and changing the order of JS plugins didn't help.

    I ended up removing turbolinks and it's working now. Probably not the best solution but it was the best thing that I achieve after some hours searching.

    So, basicly I removed that line from application.js file:

    //= require turbolinks
    
    0 讨论(0)
  • 2021-01-02 18:39

    The problem is surely in application.js: According to their Demo App, the sequence of requiring JS plugins is as follows:

    //= require jquery
    //= require jquery_ujs
    //= require turbolinks
    //= require select2
    //= require select2_locale_pt-BR
    //= require_tree .
    

    In your case, you have to load turbolinks before select2, leading to:

    //= require jquery
    //= require turbolinks
    //= require select2
    //= require jquery.ui.accordion
    //= require jquery.ui.menu
    //= require jquery.ui.datepicker
    //= require common
    //= require posts
    //= require twitter/bootstrap
    //= require owl.carousel
    //= require vendor_js
    //= require_tree .
    
    0 讨论(0)
  • 2021-01-02 18:50

    I never encountered this issue while using rails 4.1.5 & select2-rails 3.5.9.3. However, after upgrading rails to 4.2.4, I get the same exact error described in this question:

    Uncaught TypeError: $(...).select2 is not a function

    I was able to resolve the error by removing the "select2-rails" gem from my Gemfile and replacing it with:

    source 'https://rails-assets.org' do
      gem 'rails-assets-select2', '~> 4.0.0'
    end
    
    0 讨论(0)
提交回复
热议问题