Search Form In Rails — only 3 mins!

THERESA CYRUS
Nov 7 · 4 min read

Last week as I was hammering away at my rails project , everything was going well and as usual that tends to be when the code monster decides to loom over our hard working successful code with it’s new ideas (as if anyone asked) and features. Because I was feeling sooo confident I decided to listen to that monster, it spoke “How the heck are you going to be able to look up a patient on your index page? Do you reallllly wanna just scroll through all of it to find them? absurd! ”

I began to look at my project differently and lean on the code monster (our conscious) to see if a search box should be implemented and if it made sense for what I was trying to do. Generally we learn about so many great new features out there (endless!) but we are unsure when and how to use it or even what the heck the source code is even saying to us. I digress, so when would you need a search box? Whenever you would like to lookup content on your page, you can place a search box on any view page (show, index ,etc..), just place it where it makes sense.


Life without a search box: Me searching for my patients (obviously).

After searching the interwebs I came across some great medium posts, as per usual because this community is so great. Sadly with medium posts I don’t tend to understand most of them, their code doesn’t always work with my code and the snippets aren’t always explained in a way I understand it. I’m all for copying some code but if I don’t understand it how can I implement it? So from these awesome blogs I’ve constructed a easy peasy tutorial to make your search boxes work anywhere!

Here is the code you will need and where to place it, after we’ll break down what it means.

STEP 1:

This belongs in your view file.

So here we’re using a form_tag to create a search box for your view file. It requires similar syntax you might have noticed if you ever created a form with form_tag. On line 3 we added the path for the form which is the patients index page so that I can look up a patient(patients_path). The method is a GET because we’re getting information by asking the search box to GET this specific patient. On line 4 we have the text_field_tag that uses the model we’re referring to along side the params for the patient model (such as their attributes). Finally on line 5 we have our submit tag and something new we haven’t seen before name: nil . The reason for adding this line of code is only something you will notice after you submit your search. The URL in the browser will have some extra tidbits that end with:

commit=Search

The name: nil will remove this from the URL.

STEP 2:

Seeing as we will be working with the index in your controller thats where you’ll add the additional code to get your search form working. We will need to create some conditional logic so the index searches for the patient.

The code will see if params[:patient] exist, if so it will move to the Patient.where logic that has some fancy SQL lite syntax in which will be saved to an instance of @ patients . But what is the Patient.where syntax actually telling us? Luckily API dock has us covered, take a look to understand how to use the .where method!

Long story short the the .where() will access the Patients table and where the attribute/column name is, it will compare if it is LIKE what was searched, if so it then grabs that patient information with the matching column info.

SELECT “patients”.* FROM “patients” WHERE (name LIKE ‘%%’) LIMIT ?

SQL may spit something like this out, in the patients table select all from patients where the name is like the input search.

Line 7 is our else statement which will display a list of all of patients.

Phew! There’s so much information and ways to do things so don’t be afraid to experiment. If you found this helpful great! Also check out these great Medium blogs which inspired this post, happy coding!

Special Thanks To: Jonathan Zylberberg and Yassi Mortensen

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade