带有自动完成功能的Rails标记



我正在努力完成这篇关于标记的文章https://medium.com/@sherzelsmith/add-a-filter-multiple-tag-system-with-autocomplete-to-your-rails-model-in-rails-5-1bf88cd53e9
我的问题是,我需要使不存在的创建成为可能,但目前,如果你试图用新标签(目前不存在(填充字段,它会清除字段,因此这种方法无法创建新标签。这篇文章的作者部署了这个功能的演示,所以我将把它留在这里,以便更好地理解我所说的内容。https://blogit-ss.herokuapp.com/posts/new

<div class = 'col-md-8 offset-2'>
<h1 class = "text-center">New Tag</h1>
<%= simple_form_for @product, url: product_path(@product) do |f| %>
<p><small>Tags: <%= raw @product.tags.map(&:name).map { |t| link_to t, tag_path(t) }.join(', ') %></small</p>
<p><%= f.input :tag_ids, collection: Tag.order(:name), include_blank: true, input_html: { multiple: true, class: 'chosen-select' } %></p>
<%= f.submit "Next", class: 'btn btn-primary' %>
<% end %>
</div>

所以,也许这里有人对如何避免"无结果匹配"并让表单接受新标签有什么建议?就像它在Stackoverflow上工作一样。
唯一离我的目标最近的方法是form_for中的text_field:

<%=f.text_field:tag_list,collection:tag.order(:name(,include_blank:true,input_html:{multiple:true,class:‘selected-select’}%>

允许键入用逗号分隔的标记名,但不需要自动完成。

嗨,我添加了另一个可标记的下拉列表这是工作链接

添加可自定义标签的工作链接

下面是GitHub 上的代码链接

可自定义下拉代码的Github链接

因此,Jad Chahine对类似问题的回答涵盖了这一问题的最佳解决方案
https://stackoverflow.com/a/36350998/10253611


只需将引导标记输入库添加到我的项目中,并将data角色传递到我的输入字段中(尽管目前还没有自动完成,但很适合启动(:

<div class = 'col-md-8 offset-2'>
<h1 class = 'text-center'>New Blog Post</h1>
<%= simple_form_for @product, url: tag_link_product_url(@product), remote: true do |f| %>
<p><small id='tag_links'>Tags: <%= raw @product.tags.order(:name).map { |tag| link_to tag.name, products_path(q: { tags_id_eq: tag.id }) }.join(', ') %></small</p>
<%= f.text_field :tag_list, collection: Tag.order(:name), 'data-role': 'tagsinput', input_html: { multiple: true, class: 'chosen-select' } %>
<%= f.submit "Next", class: 'btn btn-primary' %>
<% end %>
</div>

最新更新