Adding a Basic Search Form in Rails

Step 1: Set Up Your Rails App

Step 2: (M) Add Method to Model

  • Self: We chose “self” for this method because the goal is to identify the instances that match our parameters by calling upon the fish class
  • Search: Search is the params[:search] that we take in as an argument given to us from our form implemented on the sushi index page.
  • Sushi_type: After our first If Statement, we then sift through all of our sushi instances using find_by until we either find an object with the name attribute that matches our search parameters or there are no instances left to search. In the event of no matching instances, we enact our Else Statement and return all of the fish instances per normal index page procedure.

Step 3: [C] Call on Method in Controller

Step 4: Permit Search Parameter in Strong Params

Step 5: [V] Add Search Form to Index Page

  • Form_tag: Form_tag is one of the few different embedded Ruby form options we have in Rails. Form_tag is especially useful when we do not want to edit any of our model attributes or aren’t calling on a particular instance. In this case, we want to sort our data and call on the index path again, which form_tag is excellent for. You can read more about the many different types of forms tags in Rails in the documentation.
  • Label_field: This is our label tag.
  • Text_field_tag: This tag is critical because it will hold our search params, which we used extensively. It’s important to note that we used the text_field_tag instead of text_field, which can manipulate our attributes.
  • Submit_tag: Our submit tag is simple — the first attribute is the displayed text on the button. The :name => nil attribute enables us to remove the commit keyword from our search query in the URL.

Step 6: Test Out the Search

--

--

--

We are a 501(c)(3) non-profit organization dedicated to inspiring women to excel in technology careers. https://www.womenwhocode.com/

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Scaling to New Heights

How should you pin your npm dependencies and why?

What Is Container Orchestration? Describing In 200 Words

7 VSCode extensions to make development comfortable

Flow Efficiency: The Hidden Truth of Agile Teams

Integrate JWT in SpringBoot to realize Token verification (Additional notes)

Over The Wire Bandit L

Cloud-scale file transfer

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
Women Who Code

Women Who Code

We are a 501(c)(3) non-profit organization dedicated to inspiring women to excel in technology careers. https://www.womenwhocode.com/

More from Medium

Ruby on Rails: Scopes

How to Create a Ruby on Rails App

Building React.js App With Ruby on Rails API

All the Code I Didn’t Write