0

我正在尝试在我的项目中实现 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 字段。我们怎么能做到这一点?任何帮助表示感谢谢谢!

4

1 回答 1

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>
于 2016-01-02T12:37:29.127 回答