本文介绍了select2-rails + act_as_taggable_on rails 4 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用中安装了 select2-rails 和 act_as_taggable_on gem.我设置了 act_as_taggable_on 并且它可以工作(我从控制台进行了测试).但是,当我尝试创建视图以便用户可以添加新标签时,它不起作用.

我想做这样的标记支持:https://select2.github.io/examples.html#tokenizer.

这是我的设置:

apps/assets/application.js

$(document).ready(function(){$(".multiple-input").select2({主题:引导程序";标签: 真实;标记分隔符:[',']})});

apps/views/profiles/new.html.erb

<%= form_for @profile, url: user_profile_path do |f|%><%= f.text_field :name, placeholder: "Full Name" %><%= f.text_field :skill_list, placeholder: "Skills", class: 'multiple-input' %><%结束%>

当我在浏览器上打开配置文件/新文件时,skill_list 文本字段显示为原样,而不是使用 select2 标记系统.

我的代码一定有问题或缺失.请帮忙.

更新 1

我将代码更改为:

运气不好.

所以,我安装了简单的表单 gem,因为基于这篇文章 (http://onewebstory.com/notes/user-friendly-tagging-on-rails) select2 rails 不适用于 select 标签.

这是我当前的代码:

apps/views/profiles/new.html.erb

<%= simple_form_for(@profile, url: user_profile_path) do |f|%><div class="form-inputs" %><%= f.input :name, placeholder: '全名' %><%= f.input :skill_list, input_html: { class: 'multiple-input' } %>

<%结束%>

apps/assets/application.js

$(document).ready(function() {$('input.multiple-input').each(function() {$(this).select2({主题:'引导',标签: $(this).data('tags'),标记分隔符:[',']});});});

select2 正在工作,我可以使用 select2 搜索和下拉列表.但是我希望它是标记输入,并且每次用户输入逗号 (,) 就像在这里一样临时存储:(https://select2.github.io/examples.html#tokenizer)

解决方案

现在可以使用了.我用这个代码做到了:https://stackoverflow.com/a/33035355/5081949

这是我现在的代码:

app/views/profiles/new.html.erb

app/assets/application.js

$(document).ready(function() {$('.multiple-input').each(function() {$(this).select2({标签: 真实,tokenSeparators: [','],主题:'引导',占位符:'用逗号分隔'});});});

也在 apps/controllers/profiles_controller.rb 中添加了强参数

def profile_paramsparams.require(:profile).permit(:name, Skill_list: [])结尾

I installed select2-rails and act_as_taggable_on gem to my app. I setup the act_as_taggable_on and it works (I tested from the console). However, when I tried to create the view so that user can add new tag, it's not working.

I want to make a tagging support like this: https://select2.github.io/examples.html#tokenizer.

Here is my setup:

apps/assets/application.js

$(document).ready(function(){
  $(".multiple-input").select2({
    theme: "bootstrap";
    tags: true;
    tokenSeparators: [',']
  })
});

apps/views/profiles/new.html.erb

<%= form_for @profile, url: user_profile_path do |f| %>
  <%= f.text_field :name, placeholder: "Full Name" %>
  <%= f.text_field :skill_list, placeholder: "Skills", class: 'multiple-input' %>
<% end %>

When I open the profiles/new on my browser, the skill_list text field show as the way it is instead of using select2 tagging system.

There must be something wrong or missing with my code. Please help.

Update 1

I changed the code to:

<%= f.select :skill_list, placeholder: "Skills", class: 'multiple-input' %>

No luck.

So, I installed simple form gem because based on this article (http://onewebstory.com/notes/user-friendly-tagging-on-rails) select2 rails won't work with select tag.

Here is my current code:

apps/views/profiles/new.html.erb

<%= simple_form_for(@profile, url: user_profile_path) do |f| %>
  <div class="form-inputs" %>
    <%= f.input :name, placeholder: 'Full Name' %>
    <%= f.input :skill_list, input_html: { class: 'multiple-input' } %>
   </div>
<% end %>

apps/assets/application.js

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

The select2 is working, I can use the select2 search and dropdown. However I want it to be tagging input and every time the user inputs comma (,) temporary stored just like in here :(https://select2.github.io/examples.html#tokenizer)

解决方案

It's working now. I did it with this code: https://stackoverflow.com/a/33035355/5081949

Here is my code now:

app/views/profiles/new.html.erb

<%= f.input :skill_list, input_html: { class: 'multiple-input', multiple: "multiple" }, collection: @profile.skill_list %>

app/assets/application.js

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

also in apps/controllers/profiles_controller.rb I added strong params

def profile_params
  params.require(:profile).permit(:name, skill_list: [])
end

这篇关于select2-rails + act_as_taggable_on rails 4 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 12:58