带有select2的Rails 5 ask_as_taggable [英] Rails 5 ask_as_taggable with select2

查看:64
本文介绍了带有select2的Rails 5 ask_as_taggable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论在text_field还是select中,我都看不到rail2 5中的select2的效果.有谁知道为什么不填充tag_list?我可以看到tag_list具有值,但是正如您在下面的快照中所看到的,它显示未找到结果".我尝试了几种选择后,select2是否可以与Rails 5一起使用?

I cannot see the effect of select2 in rails 5 form whether it's a text_field or select. does anyone know why the tag_list is not being populated. I can see that the tag_list has values but as you see in the snap below, it shows "No results found". Does select2 work with Rails 5 as I've tried several options?

表格:全选将在框中显示所有值

form: Select all will display all values into the box

<%= f.label :tags, "Tags (separated by commas)" %>
<%= f.text_field :tag_list, value: f.object.tag_list.each { |e| e} %>
<input type="checkbox" id="checkbox" >Select All

我使文本字段变得简单而不是select2,并且能够通过将:tag_list添加到强参数中来在DB中添加标签.

I made the text field simple instead of select2 and was able to add tags in DB by adding :tag_list to strong params.

application.js-

application.js -

注意:select2无效o我使用了在Google某处看到的select2-full.

Note: select2 didnot work o I used select2-full which I saw somewhere in Google.

//= require underscore
//= require select2-full

select2的JS代码

JS code for select2

$(function() {
$('input#post_tag_list').select2({tags:[], placeholder: "Choose a Tag", allowClear: true})

});

现在,我检查了正常的HTML& JS甚至可以与select2(或select2-full)一起使用

Now, I checked with normal HTML & JS it even worked with select2(or select2-full)

<select multiple id="e1" style="width:300px">
   <option value="A">Apple</option>
   <option value="B">Banana</option>
   <option value="G">Grapes</option>
  </select>

  <input type="checkbox" id="checkbox" >Select All 

对应的JS代码是:

$("#checkbox").click(function(){
if($("#checkbox").is(':checked') ){
  $("#post_topic_list > option").prop("selected","selected");
  $("#post_topic_list").trigger("change");
}else{
  $("#post_topic_list > option").removeAttr("selected");
  $("#post_topic_list").trigger("change");
}

});

有人可以指出普通的HTML select-input是否可以与select2一起使用吗,为什么它在rails 5中没有相同的功能?我尝试了bootstrap_form_for和form_for.我什至读过有关turbolinks问题的信息...如何通过不加载select2的turbolinks解决该问题?

Can someone point out if normal HTML select-input works with select2 why does it not have same functionality in rails 5? I tried with both bootstrap_form_for and form_for. I even read about turbolinks issue...how to tackle the issue with turbolinks not loading the select2?

推荐答案

我通过更改文本字段来选择来解决它:

I solved it by changing text-field to select:

<%= f.select(:tag_list, Tag.all.order(:name).collect { |a| [a.name, a.id]}, {}, id: "tag_list", label:'Tags', :multiple => true)%>

JS代码是:

$(function() {
$("#tag_list" ).select2();

});

是的,请在应用程序/模型中添加一个名为Tag.rb的文件. gem仅安装表格,但不创建模型.

and yes, do add a file called Tag.rb in app/models. The gem only installs tables but doesn't create model.

class Tag < ApplicationRecord
   validates_presence_of :name
end

这篇关于带有select2的Rails 5 ask_as_taggable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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