Rails acts like-taggable-on with select2 (and simple_form)

I would like to have select2 drop2 tags, where I could select some existing tags and add new ones.

I tried many different ways, and I either do not get box select2, or only one value is passed (the last).

This is the closest I received (passes the last value):

<%= f.input :tag_list, collection: @model.tags.map { |t| t.name }, input_html: { :style=> 'width: 300px', class: "taggable", data: { placeholder: "Tags" }} %> 
+5
source share
3 answers

As I mentioned earlier, the regular select2 library now only uses selected input types, but it works with input types when using the select2-full library.

So, this was the way out of one problem.

I had problems passing only one value. I actually copied / pasted the code from one of the examples, and that was wrong. It was pointed out that strong parameters should include: tag_list, which is obvious, but actually it is {tag_list []}, which is necessary to accept all values.

Now works like a charm.

+3
source

For me, this works:

Coffee script:

  $( ".tags" ).select2 theme: "bootstrap", tags: true tokenSeparators: [','] 

and in view:

 = f.input :tag_list, input_html: { class: 'tags', multiple: "multiple" }, collection: @video.tag_list 

The important part of multiple: "multiple"

And, of course, as Mitya said, do not forget to add {tag_list: []} to your strong controller parameters.

And as an example, if you want your tags to be offered in a drop-down list, you can do this:

 = f.input :tag_list, input_html: { class: 'tags', multiple: "multiple" }, collection: ActsAsTaggableOn::Tag.most_used(30), value_method: :name 
+2
source

Try this, hope this works for you.

 = f.input :tag_list, class: "taggable",data: {options: @model.tags.map { |t| t.name }} $(".taggable").select2( tags: $('.taggable').data('options') width: "252px" ); 
+1
source

All Articles