Build a Simple Search with the simple_form Gem in Rails 5
Searching in Rails 5 is one of those things that always starts out simple “oh, I’ll just add a search bar to the site” and eventually ends up in a slew of PostgreSQL queries, tears, and eventually.. defeat. A search can be a basic filter of your model, but creating a search that is efficient, fast, and actually gets the results you want to show is much more complex. Think of how you felt last time you searched for something on Bing instead of Google. A good algorithm can make all the difference.
I’ve seen many search tutorials for Rails 5 that use form_tag
, HTML forms, or form_for
, but there are not as many resources for simple_form
, even though simple_form
has become the gold standard of Rails forms. Besides, both form_for
and form_tag
are on their way to be deprecated after Rails 5.2 and replaced by form_with
.
Here’s a quick overview of how you can add basic search functionality to your app using the simple_form
gem!
Let’s get started!
1. Setup your Rails App
I’m creating a basic rails application, without Webpacker, but using PostgreSQL as my…