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 。
这是我的设置:
应用程序/资产/ application.js中
$(document).ready(function(){ $(".multiple-input").select2({ theme: "bootstrap"; tags: true; tokenSeparators: [','] }) });
应用程序/视图/型材/ new.html.erb
当我在浏览器上打开profiles / new时,skill_list文本字段显示为它的方式,而不是使用select2标记系统。
我的代码肯定有问题或遗漏。 请帮忙。
更新1
我将代码更改为:
没运气。
所以,我安装了简单的表单gem,因为基于这篇文章( http://onewebstory.com/notes/user-friendly-tagging-on-rails),select2 rails不适用于select标签。
这是我目前的代码:
应用程序/视图/型材/ new.html.erb
应用程序/资产/ application.js中
$(document).ready(function() { $('input.multiple-input').each(function() { $(this).select2({ theme: 'bootstrap', tags: $(this).data('tags'), tokenSeparators: [','] }); }); });
select2正在工作,我可以使用select2搜索和下拉列表。 但是我希望它标记输入,每次用户输入逗号(,)临时存储就像在这里:( https://select2.github.io/examples.html#tokenizer )
它现在正在运作。 我用这段代码完成了它: https : //stackoverflow.com/a/33035355/5081949
这是我的代码:
应用程序/视图/型材/ new.html.erb
<%= f.input :skill_list, input_html: { class: 'multiple-input', multiple: "multiple" }, collection: @profile.skill_list %>
应用程序/资产/ application.js中
$(document).ready(function() { $('.multiple-input').each(function() { $(this).select2({ tags: true, tokenSeparators: [','], theme: 'bootstrap', placeholder: 'Separated by comma' }); }); });
同样在apps / controllers / profiles_controller.rb中我添加了强大的参数
def profile_params params.require(:profile).permit(:name, skill_list: []) end