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

后端 未结 3 1400
梦毁少年i
梦毁少年i 2021-01-05 09:18

I would like to have a select2 dropdown of tags, where i could select multiple existing tags and add new ones.

I have tried many different ways and i either don\'t g

3条回答
  •  悲哀的现实
    2021-01-05 09:47

    For me work this:

    Coffee script:

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

    and in the view:

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

    Important part is multiple: "multiple"

    And of course, how Mitja said, don't forget to put { tag_list: [] } to your controller strong parameters.

    And as example, if you want to have your tags as suggestions in the dropdown, you could do this:

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

提交回复
热议问题