Searching is one of the most common features found on virtually any website. There are numerous solutions out there for easily incorporating search into your application, but in this article I’ll discuss Postgres’ native search in Rails applications powered by the pg_search gem. On top of that, I’ll show you how to add an autocomplete feature with the help of the select2 plugin.
I’ll explore three examples of employing search and autocomplete features in Rails applications. Specifically, this article covers:
The source code can be found at GitHub.
Go ahead and create a new Rails application. I’ll be using Rails 5.0.1, but most of the concepts explained in this article apply to older versions as well. As long as we’re going to use Postgres’ search, the app should be initialized with the PostgreSQL database adapter:
rails new Autocomplete –database=postgresql
Create a new PG database and setup config/database.yml properly. To exclude my Postgres username and password
from the version control system, I’m using the dotenv-rails gem:
group :development do
To install it, run the following:
$ bundle install
and create a…