简体   繁体   English

Rails 5,带有Select2-rails的简单表单

[英]Rails 5, Simple Form with Select2-rails

I'm trying to figure out how to use select2 with simple form in my rails 5 app. 我试图弄清楚如何在我的Rails 5应用程序中以简单的形式使用select2。

I am using acts as taggable on in the app. 我正在使用可在应用中标记的行为。

I have installed this gem in my gem file: 我已经在我的gem文件中安装了这个gem:

gem 'select2-rails'

I tried to install this gem in my gemfile, but I get an error saying that it cant be found. 我试图在我的gemfile中安装该gem,但是却收到一条错误消息,提示找不到它。 I'm wondering if it isnt compatible with Rails 5 (given the last update was 2015). 我想知道它是否与Rails 5不兼容(鉴于最近的更新是2015年)。

# gem 'select2_simple_form'

My objective is to make a form input field that users can complete by selecting 5 predefined tags available on the tagged model. 我的目标是通过选择标记模型上可用的5个预定义标记,使用户可以完成表单输入字段。

I've tried the solutions put forward in this post and this post and I've tried every example in this documentation stream. 我已经尝试了本文和本文中提出的解决方案,并且已经尝试了本文档流中的每个示例。 I cant get any of them working. 我不能让他们任何一个工作。

In my application.js, I have: 在我的application.js中,我有:

//= require select2

I have also tried each of these: 我还尝试了以下每种方法:

// $(document).ready(function() {
//   $(".js-example-basic-multiple-limit").select2({
//   maximumSelectionLength: 2
//   });
//
// });

$(document).ready(function() {
  $('.multiple-input').each(function() {
    $(this).select2({
      tags: true,
      tokenSeparators: [','],
      theme: 'bootstrap',
      placeholder: 'Separated by comma'
      });
    });
});

$(document).ready(function() {
  $('.input-row #proposal_randd_field_list, .input-row #proposal_randd_field_list').select2({tags:[]})
});

I cant get this to work at all. 我根本无法使它正常工作。 When I inspect the google console - I don't get any js errors, but I also don't get a working select 2 functionality. 当我检查Google控制台时-我没有收到任何JS错误,但是我也没有得到有效的select 2功能。 I can just select in the ordinary way that simple form allows from a drop down menu. 我可以从下拉菜单中以简单形式允许的普通方式进行选择。

My objective is to have the content of my Randd::Field model (which has an attribute called :title; those titles are the tags on the proposal model and stored in a randd_field_list in that proposal model) accessible as a searchable, drop down list as shown here (under the heading Loading remote data). 我的目标是使Randd :: Field模型的内容(其属性称为:title;这些标题是投标模型上的标记,并存储在该投标模型中的randd_field_list中)可作为可搜索的下拉列表访问如图这里 (在标题加载远程数据)。

Does anyone know how to achieve this in rails using simple form. 有谁知道如何使用简单的形式在Rails中实现这一目标。 The documents don't line up at all. 文件根本没有对齐。

My current attempt is: 我目前的尝试是:

<%= f.collection_select :randd_field_list, Randd::Field.order(:title),  :title, :title, {:selected => @proposal.randd_field_list, :include_blank => true}, input_html: {:class => 'multiple-input', :multiple => true} %>

The above does not allow me to select multiple tags (with any variation of the application.js function enabled). 上面不允许我选择多个标签(启用了application.js函数的任何变体)。

I have also tried: 我也尝试过:

<!-- <select class="js-example-basic-multiple" multiple="multiple"> -->
<!--<input type="hidden" id="ajax-example" /> -->

<%#= f.label :randd_field %>
<%#= f.collection_select :randd_field_list, Randd::Field.order(:title),  :title, :title,  input_html: { class: 'multiple-input', multiple: "multiple" } %>

<%#= f.collection_select :randd_field_list, Randd::Field.order(:title),  :title, :title, {:selected => @proposal.randd_field_list, :include_blank => true} %>
<!-- </select> -->
  <!-- <select class="js-example-basic-multiple" multiple="multiple"> -->
     <!-- <option value="AL">Alabama</option>
     <option value="WY">Wyoming</option>
 </select> -->
        <%#= f.text_field :randd_field_list, input_type: "textbox", name:"proposal[randd_field_list][]", html_options: { style: 'width: 100%' } %>
        <%#= f.collection_select :randd_field_list, Randd::Field.order(:title),  :title, :title, {} %>
        <%#= f.input 'randd_field_list',
       options_from_collection_for_select(@randd_fields, :title, :title),
       multiple: true %>
       <%#= f.collection_select 'randd_field_list',
      options_from_collection_for_select(@randd_fields, :title, :title),
      multiple: true %>
       <%#= f.input :randd_field_list,:collection => @randd_fields,:label_method => :title,:value_method => :title,:label => "Fields" ,:include_blank => false, :multiple => true %>

None of these work. 这些都不起作用。

I ran across your post looking for some Rails 5 update info. 我跑遍了您的帖子,寻找一些Rails 5更新信息。 I'm a newbie, but I'm grappling with the problem of too many small gems. 我是新手,但我正在解决太多小宝石的问题。 I'm wondering if you shouldn't give up on select2 and do what you need using just simple_form ? 我想知道您是否不应该放弃select2并仅使用simple_form来完成simple_form Just a thought because I have no idea how to handle tags as you're trying to do. 只是一个想法,因为我不知道如何在尝试操作时处理标签。

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM