1

I have implemented the functionality of adding tags to a post in Rails using chosen-rails and acts-as-taggable gems. I want to further enhance the functionality by making user to create new tag if it doesn't exist.

form.html.slim

.form-group.text-center
        = f.label :tag_ids, "Tags"
        = f.collection_select :tag_ids, Tag.order(:name), :id, :name, {}, {multiple: true}

enter image description here

I need to add a new tag if it doesn't exist.

maheshkumar
  • 395
  • 2
  • 5
  • 14
  • You would either do this through an ajax call or with [nested attributes](http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html). However you should show that you have at least tried to solve the issue or looked into a solution or this question will very likely be closed. – max Oct 07 '16 at 16:23
  • I just did this a few days ago with acts_as_taggable and selectize.js. If you want a solution using that let me know. – bkunzi01 Oct 07 '16 at 16:56
  • @bkunzi01 It will be great to know how you implemented it. – maheshkumar Oct 07 '16 at 17:09

1 Answers1

2

Ok with Selectize.js, Acts_as_Taggable, & SimpleForm:

Firstly, change your input from collection_select to a normal string input. Selectize.js will separate all of the values into tags anywhere there is a comma. This happens behind the scenes so as people add tags, it's actually inserting a long string into your database with whatever delimiter you supply. Then you need to add an id to that field so you can initialize selectize such as:

<%= f.input :nationalities, as: :string, placeholder: "Nationalities", input_html: { id: 'nationality-input'} %>

Then initialize selectize.js:

#The following line gets all tags ever posted for a user with the context 'nationalities' which we will use as options.
<%=nations = ActsAsTaggleOn::Tagging.includes(:tag).where(context: 'nationalities').uniq.pluck(:id, :name)
$(document).ready(function() {
 $('#nationality-input).selectize({
    delimiter: ',',
    persist: true,
    allowEmptyOption: false,
    options: [
       <% nations.each do |nat| %>
         {text: '<%=nat[1] %>', value: '<%=nat[1]%>' },
       <% end %>
    searchField: 'text',
    create: function(input) {
       return {
          value: input,
          text: input
     }
   } 
});
});

Make sure you have acts_as_taggable setup properly and that the corresponding model includes it. Then in your controller just save the whole string with commas and all and allow selectize to reformat it on views automagically.

bkunzi01
  • 4,504
  • 1
  • 18
  • 25
  • I was just stumbling upon this yesterday, see also my question: https://stackoverflow.com/questions/70166517/rails-acts-as-taggable-on-and-selectize ... however apart from some typos in your script, I think there are two problems: one, if you have >1 tag, the form doesn't display properly, 2) the list of tags isn't recreated dynamically if you add new tags (as the JS isn't recompiled every time that happens)... @bkunzi01 – tkhobbes Dec 01 '21 at 08:04