首页 文章

Rails使用自动完成功能进行标记

提问于
浏览
1

我正在尝试完成关于标记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>

所以也许有人在这里有任何关于如何避免'no results match'并让表单接受新标签的建议?就像它在这里工作一样,在Stackoverflow上 .
唯一一种与我的目标最接近的方式 - form_for中的text_field: ****

<%= f.text_field :tag_list, collection: Tag.order(:name), include_blank: true, input_html: { multiple: true, class: 'chosen-select' } %>
允许输入用逗号分隔的标签名称,但没有自动完成 .

2 回答

  • 0

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

    Working link for add customizable tag

    以下是GitHub上代码的链接

    Github link for the customizable drop down code

  • 0

    因此,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>
    

相关问题