我正在尝试完成有关标记https://medium.com/@sherzelsmith/add-a-filtering-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,集合:Tag.order(:name),include_blank:true,input_html:{多个:true,类:'choose-select'}%>
允许输入标记名称,并以逗号分隔,但不能自动完成。因此Jad Chahine对类似问题的回答涵盖了这一问题的最佳解决方案
https://stackoverflow.com/a/36350998/10253611
只需将bootstrap-tagsinput库添加到我的项目中,然后将data-role传递到我的输入字段中(尽管目前它没有自动完成功能,但对于启动很有利):
<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>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句