Adding in Search for Rails

Zach Cusimano
loriscode
Published in
2 min readFeb 27, 2017

Creating simple search like functionality in your rails project can be tricky compared to using something like Angular.

In this example I had a list of park names that I wanted to search through without combing the entire list.

First in the controller, setup the search params statement following your index method. This example also includes the use of pagination.

campsite_controller

Then in your model choose the fields that you would like your search method to go through. In this example the search term will be run against the name field of each park.

campsite model

Finally add in the search bar to your view!

campsite#index

Check out your page for some serious search!

index view and search filtering

// Using Rails 5

--

--