ruby-on-railsrubyjquery-select2select2-rails

select2-rails tags dropdown not working when clicked


I have the following field

<%= f.collection_select :tag_list, @tags.order(:name), :id, :name, {}, { class: 'select2', multiple: true } %>

I have added the select2-rails gem and have added the js and css in the respected files

//= require select2-full application.js *= require select2 application.css

I have also added the following to my application.js file

$(document).ready(function() {
    $('.select2').select2();
});

However, I'm getting the following enter image description here Won't even dropdown.

Rendered HTML

<select class="select2 hasCustomSelect" multiple="multiple" name="contact[tag_list][]" id="contact_tag_list" style="appearance: menulist-button; position: absolute; opacity: 0; height: 33px; font-size: 13px; width: 1563px;">
  <option value="28">ANFIM</option>
  <option value="11">BWT filter</option>
  ...
</select>

When adding the select2 js in the application.js I also get this enter image description here

app.js

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, or any plugin's
// vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery.min
//= require jquery_ujs
//= require prototype
//= require moment.min
//= require fullcalendar
//= require lib_ext
//= require BigDecimal-all-1.0.1.min
//= require dialog
//= require widgets
//= require classes
//= require payroll
//= require account_context_menu
//= require plan_table
//= require requirements
//= require help
//= require help_data
//= require jquery.contextMenu.js
//= require jquery.treetable
//= require jquery.jstree
//= require select2-full 
//= require plugins
//= require scripts
//= require additional
//= require effects
//= require clusterize.min
//= require consolidated_orders
//= require sales_invoices
//= require main
//= require journals/auto_reversal
//= require budgets/new
//= require budgets/export

$(document).ready(function() {
  $('.select2').select2();
});

Solution

  • try running

    $(function() {
        $('.select2').select2();
    });
    

    $(document).ready have been removed in jquery3

    I could not reproduce the issue, I have the same libraries as you and imported css assets aswell, except that I have //= require jquery instead of //= require jquery.min but should not be a problem try running $('.select2').select2(); directly in your js console and see if it works - if so it looks like you have a problem with your jquery ready function