chosen-rails error chosen is not a function

心不动则不痛 提交于 2019-12-24 14:35:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!