Making a Search and Filter Function in Ruby on Rails

Learn clever uses of cookies and reduce reliance on performance-impacting scripts

Sukrit Walia
Sep 20, 2019 · 4 min read
Image for post
Image for post
Photo by Christian Holzinger on Unsplash

Ruby on Rails is fantastic for easily creating a powerful and flexible back end. With the popularity and flexibility of JavaScript, Ruby is not often used to create a front end. However, I argue that limiting yourself to HTML, Ruby, and CSS can help developers to learn to build better websites overall. By testing how far we can push the dynamism of our web pages without JavaScript, we can learn clever uses of cookies and reduce reliance on performance-impacting scripts.

A good way to practice using cookies with Rails is to create a simple search and sort function for a website. Let’s take a look at the components that make both of these actions possible at the same time.

The Code

For this example, we’re going to use a very basic fantasy football roster application, the full code for which can be found here in case you want to follow along with the code shown.

The above methods handle the response of players from the database. The first method, handle_search_name takes a cookie that holds the user’s search input and uses that to select the players whose names are like the search term, using the activerecord method: where @players = Player.where("name LIKE ?", "%#{session[:search_name].titleize}%")

The method handle_filters takes the currently available list of players and then filters that by the appropriate option” @players = @players.where(position: session[:filter_option])

Both of these methods rely on sessions. The session is effectively just a collection of cookies. These cookies are passed in through parameters sent back to the server as part of a get request from a form. So let's take a look at how those are sent.

The above form sends a get request back to the same page, but passes in the user's search term as a parameter called search_name.

Then in our Rails controller, we use:

The above line of code stores the passed parameter of search_name as part of our session which is then later used by the handle_search_name method. Although we could simply use the parameter to make our search work, the problem with parameters is that they do not persist between requests.

Why Sessions?

If a parameter doesn’t persist over multiple requests, when we select our filter option (which makes a request to the server) the search term for the user is blank. That’s why we have to take that parameter and store it as part of a session.

A session in Rails is effectively an encrypted set of cookies. If we don’t want users to be able to manually edit individual cookies, we use a session. In this particular case, we want to control when specific cookies persist and when they clear. For this reason, we'll put our cookies in a session rather than keep them as individual cookies.

So now that our user has given us a search term to store, we can further narrow our search using a filter. In our HTML we have a form for each filter option, let's take a look at one below:

The above code creates a drop-down menu which, when changed, will submit a request back to the server with the parameter of the option that was selected. Back on the controller side in Rails, we handle the parameter and save it to our session. Now that we have both a search term and a filter option, we have everything we need to be able to display the results.

Showing the Results

The most satisfying part is to actually get these results to show on the page.

Assuming your routes are configured correctly, all we need to do is call these search and filter handlers in your controller. This will handle the checking of the parameters every time, store the appropriate parameters in our session, and then organize the data from our database.

Then in our HTML, we have to create the elements that were returned in our newly-sorted set of data.

Viola! The end result should look similar to the gif below:

Image for post
Image for post
Gif Credit @mrwerner392

Using the same principles applied in this project, we can push Rails to feel more interactive as a front end as well as train ourselves to better utilize tools such as cookies.

Better Programming

Advice for programmers.

By Better Programming

A weekly newsletter sent every Friday with the best articles we published that week. Code tutorials, advice, career opportunities, and more! Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store