我正在尝试在我的项目中实现select2功能。它没有正确渲染。我正在粘贴我的代码,请告诉我哪里出错了
<div class="form-group">
<label for="userInputCollegeName">College Name</label>
<%= f.collection_select(:college_id, College.all, :id, :name, {:prompt => 'Select the college'}) %>
</div>
<script>$(document).ready(function() {
$("#user_college_id").select2({theme: "bootstrap"});
});</script>
在我的html中,collection_select的ID为user_college_id
。
我正在添加图片:
我知道第一个选择是因为collection_select标签而第二个字段是因为select2,但我不需要第一个。
我只需要select2字段。我们怎么做?感谢任何帮助谢谢你!
答案 0 :(得分:0)
您的
可能存在问题f.collection_select(:college_id, College.all, :id, :name, {:prompt => 'Select the college'})
尝试将其更改为
f.collection_select(:college_id, College.all, :id, :name, {:prompt => 'Select the college'}, {class: "user-college-select"})
并在javascript
内:
<script>
$(document).ready(function() {
$(".user-college-select").select2({theme: "bootstrap"});
});
</script>