在Rails中选择2无法正确渲染

时间:2016-01-02 12:05:36

标签: ruby-on-rails layout select2-rails collection-select

我正在尝试在我的项目中实现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

我正在添加图片:

enter image description here

我知道第一个选择是因为collection_select标签而第二个字段是因为select2,但我不需要第一个。

我只需要select2字段。我们怎么做?感谢任何帮助谢谢你!

1 个答案:

答案 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>