argerim / select2-rails

Integrate Select2 javascript library with Rails asset pipeline
https://github.com/argerim/select2-rails
965 stars 374 forks source link

unexpected token and select2 is not a function errors #163

Closed benkoshy closed 6 years ago

benkoshy commented 8 years ago

Hi folks

I'm getting two errors:

  1. Uncaught SyntaxError: Unexpected token <
  2. Uncaught TypeError: $(...).select2 is not a function

I have no idea what this means.

here is my application.js file:

//= require jquery //= require jquery.turbolinks //= require jquery_ujs //= require dataTables/jquery.dataTables //= require turbolinks //= require select2 //= require_tree .

Here is form:

<%= form_for(@job, :remote => true) do |f| %>
  <%= render "shared/errors", object: @job %>

<div class="field">
  <%= label :client, :client_id %><br>
  <%= collection_select :client, :client_id, Client.order(:name),:id,:name, {include_blank: true},{:id=>'client_id_select'} %>
</div>

**<label for="id_label_projects-searchable-drop-down">
  Search by Project Number 
</label>
  <select class="searchable-drop-down" id="projects-searchable-drop-down" >    
  </select>**

<div class="field">
  <%= label :project, :project_id %><br>
  <%= collection_select :project, :project_id, [],:id,:name, {include_blank: true},{:id=>'project_id_select'} %>
</div>
<div class="field">
  <%= label :building, :building_id %><br>
  <%= collection_select :building, :building_id, [],:id,:name, {include_blank: false},{:id=>'building_id_select'} %>
</div>
<div class="field">
  <%= label :level, :level_id %><br>
  <%= f.collection_select :level_id, [],:id,:name, {include_blank: false},{:id=>'level_id_select'} %>
  <%#= f.collection_select(:job_type_id, JobType.order(:name), :id, :name)%>
</div>
<div class="field">
  <%= label :open, :open %><br>
  <%= f.check_box :open %>
</div>
<div class="field">
  <%= label :date_closed, :date_closed %><br>
    <%= f.date_select :date_closed, :order => [:day, :month, :year] %>
</div>
        <div class="field">
        <%= f.label :hours %>
        <%= f.number_field :hours %>
      </div>
        <div class="field">
        <%= f.label :panels_created %>
        <%= f.number_field :panels_created, autofocus: true %>
      </div>
      <div class="field">
        <%= f.label :job_type, :job_type_id %>
        <%= f.collection_select(:job_type_id, JobType.order(:name), :id, :name)%> <!--# when i do this it says unpermitted parameter-->
      </div>

<%= f.submit %> </br>
      <%= link_to "Cancel", jobs_path, class: 'button' %>
<%end%>

It's just not working for me. Those two errors are showing on firebug and I don't know how to proceeed. is it a bug or something i'm doing wrong on my end?

ghost commented 7 years ago

Hi,

I don't know if it will resolve your problem, but with kind of similar one I tried to use //= require select2-full instead of //= require select2 and everything was fine again, so maybe you should try... ?

johnhenriksson commented 7 years ago

I've got the same problem.. But it's only in production. In development it works fine!

I've tried //= require select2-full but it still gives me the error. Only in production though.

Uncaught TypeError: $(...).select2 is not a function
    at HTMLDocument.<anonymous> (new:87)
    at HTMLDocument.dispatch (application-f67d75a….js:2)
    at HTMLDocument.m.handle (application-f67d75a….js:2)
    at Object.trigger (application-f67d75a….js:2)
    at pt.fn.init.triggerHandler (application-f67d75a….js:2)
    at Function.ready (application-f67d75a….js:2)
    at HTMLDocument.a (application-f67d75a….js:1)
argerim commented 7 years ago

@BKSpurgeon move //= require jquery_ujs to top

//= require jquery //= require jquery_ujs //= require jquery.turbolinks //= require dataTables/jquery.dataTables //= require turbolinks //= require select2 //= require_tree .

LucasCioffi commented 6 years ago

Moving //= require select2 above //= require_tree . in application.js solved the problem for me.

shunwen commented 6 years ago

@argerim @BKSpurgeon Hi, do you guys still have this problem? If it only happens on production, then check if there is any error in js files required before select2. The error causes the application.js not fully loaded, that's why very often moving //= require select2 upward could solve this issue.

AlexGravenSparkfly commented 6 years ago

Still having this issue. Can we fix it?

benkoshy commented 6 years ago

@AlexGravenSparkfly @shunwen Unfortunately I cannot help you at this stage: I removed the gem entirely from my project; i may return and have another attempt at it in the future.