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 タグが原因で、2 番目のフィールドは 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 に答える