问题
I am using chosen-rails for integration of chosen.js with rails asset pipeline
I have included in my Gemfile
gem 'chosen-rails'
then
bundle install
in my application.js I have
//= require jquery
//= require jquery_ujs
//= require chosen-jquery
//= require_self
in my application.css I have
*= require chosen
The page loads the js as required
/assets/jquery.js?body=1
/assets/jquery_ujs.js?body=1
/assets/lib/select-parser.js?body=1
/assets/chosen.jquery.js?body=1
But I I try to use the function chosen() it gives the error chosen is not a function
$('#whatever').chosen();
I have tried the above in the footer. inside Jquery ready.. inside firebug console.. nothing seems to work. What can possibly be the issue ???
回答1:
My issue was that jQuery was included both in application.js
(v1.11.1) and also in my layout via CDN (v2.1.4).
<script src="//cdn.jsdelivr.net/jquery/2.1.4/jquery.min.js"></script>
I kept the CDN version at the beginning of my header, removed //= require jquery
from application.js
and it worked fine.
回答2:
I realized the problem was not with jQuery or asset pipeline or the chosen-rails gem instead it was a server configuration issue and wrong files were being served due to bad nginx configuration.
回答3:
I ran into the same issue and it brought me here. For me I was including both jquery and chosen twice in my application. Doubly including either may cause this error.
来源:https://stackoverflow.com/questions/10874093/chosen-rails-error-chosen-is-not-a-function