Rails 6, Stimulus and Select2

Rails 6, Stimulus and Select2

This is just a quicky, to help out anyone using Rails 6, or any Stimulus-enabled Rails project and Select2, the amazing jQuery plugin for better select boxes.

add select2 & select2-bootstrap-theme package in web-pack

yarn add select2
yarn add select2-bootstrap-theme

Simple Select2 + Stimulus Controller

select2_controller.js

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() {
    $('.content-search').select2();
  }
}

Replace f.select dropdown according to select2 specifications

<div class="form-group" data-controller='select2'>
  <%= f.select :user_id, User.all.map { |user| user.name }, {include_blank: false, required: true, include_hidden: false}, class: 'form-control content-search' %>
</div>

I hope that helps someone. Thanks :).

I’d love to hear thoughts or comments around this. Feel free to email me at ronakabhattrz@gmail.com or hit me up on Twitter, @ronakabhattrz .

Vineet Rawat

Software Engineer at NAYAN | Backend | Ruby on Rails

3 å¹´

Worked for me...Thanx ??

赞
回复
Sanchit Samuel

SSDE-2 at Sopra Banking Software | Java | Spring boot | Ruby | Ruby on Rails | Rspec | Postgres | Micro Services

3 å¹´

Very nice! Thanks.

Sathibabu P

Senior Software Engineer at iCentris

4 å¹´

This will help me

Hardik Joshi

Helping startups || Expert Mobile App developer || Team Lead || entrepreneur || ReadyToHelp

4 å¹´

Helpful!

要查看或添加评论,请登录

Ronak Bhatt的更多文章

  • Add multiple Subdomains in Ruby On Rails 7

    Add multiple Subdomains in Ruby On Rails 7

    Subdomains are a great way to organize different parts of your app. You might have an subdomain, subdomain, etc.

    1 条评论
  • Rails 6, Stimulus events

    Rails 6, Stimulus events

    An action is a connection between: a controller method the controller’s element a DOM event listener…

    1 条评论

社区洞察

其他会员也浏览了