在Rails中选择2无法正确渲染 [英] Select 2 in Rails not rendering properly

查看:87
本文介绍了在Rails中选择2无法正确渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的项目中实现select2功能.它无法正确渲染.我正在粘贴我的代码,请告诉我哪里出了错

I am trying to implement select2 functionality in my project. It is not rendering properly. I am pasting my code, please tell me where it went wrong

<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.

In my html the id for the collection_select is user_college_id.

我要添加图片:

我知道第一个选择是由于collection_select标记而第二个字段是由于select2,但我不需要第一个.

I know that the first select is because of collection_select tag and the second field is because of select2, but I dont need the first one.

我只需要select2字段.我们该怎么做?感谢您的任何帮助,谢谢!

I only require the select2 field. How can we do that? Any help is appreciated thankyou!

推荐答案

您的笔记本中可能有问题

there might be problem in your

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>

这篇关于在Rails中选择2无法正确渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆