Skip to content

Instantly share code, notes, and snippets.

@philsmy
Last active December 27, 2019 05:40
Show Gist options
  • Save philsmy/f0f52e37ed4913913e2f2e63be67f165 to your computer and use it in GitHub Desktop.
Save philsmy/f0f52e37ed4913913e2f2e63be67f165 to your computer and use it in GitHub Desktop.
Basics of Select2 and Stimulus
import { Controller } from "stimulus"
import $ from 'jquery';
require("select2/dist/css/select2")
require("select2-bootstrap-theme/dist/select2-bootstrap")
import Select2 from "select2"
export default class extends Controller {
connect() {
$('.subscriber-tags').select2({
tags: true,
tokenSeparators: [',', ' ']
});
}
}
<%= form_with(model: @my_model, local: true) do |form| %>
<div class='card mt-4'>
<div class='card-header'>
Tags
</div>
<div class='card-body' data-controller='select2'>
<%= form.select :tags, SubscriberTag.all.map{ |t| [t.name,t.id]}, {include_blank: true, required: false, include_hidden: false}, class: 'form-control subscriber-tags', multiple: 'multiple' %>
</div>
</div>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment