[RUBY] Add a search function in Rails.


The ability to search for data within an application is useful for users when searching for data. Since the search function is a common function in SNS etc., let's actually implement the search function. If there is a table called user, implement the function to search for user.

1. Routing settings

It is assumed that UserModel and UsersController that handle user have been created.


  resources :users do
    get "search", on: :collection

The search action adds on :: collection to represent a collection of resources.

2. Add class method search to model


class User < ApplicationRecord
  class << self
    def search(query)
      rel = order("id")
      if query.present?
        rel = rel.where("Column name LIKE?, "%#{query}%")

You can define class methods with class << self ~ end. Define the local variable rel, and if the search word is not empty, use SQL LIKE to narrow down the target record from the corresponding column.

3. Add search action to controller


def search
  @users = User.search(params[:q])
  render "index"

The class method search defined in the User model is used here.

4. Fill out the form in view


<%= form_tag :search_users, method: :get, class: "search" do %>
  <%= text_field_tag "q", params[:q] %>
  <%= submit_tag "Search" %>
<% end %>

form_tag creates a form with a path as an argument. Since the default method is POST, we are specifying the get method. A form is created with text_field_tag, and params [: q] is specified as the second argument so that the search word "q" remains in the form even after the search.


Now you can implement the search function. There was also a part dealing with SQL, so I would like to take this opportunity to study.

Recommended Posts

Add a search function in Rails.
[Rails] Implement search function
Rails search function implementation
Add a tag function to Rails. Use acts-as-taggable-on
Let's make a search function with Rails (ransack)
How to make a follow function in Rails
Implement application function in Rails
Rails fuzzy search function implementation
[Rails] Implement User search function
Search function using [rails] ransack
Implement follow function in Rails
[Rails 6] Implementation of search function
[Rails] Creating a search box
How to write a date comparison search in Rails
I want to define a function in Rails Console
[Rails] Keyword search in multiple tables
Implement simple login function in Rails
Create a new app in Rails
Implement a contact form in Rails
Establish a search bar in Rails ~ Join multiple tables to search
Implement CSV download function in Rails
[Rails] Implemented a pull-down search function for Active Hash data
[Rails] A simple way to implement a self-introduction function in your profile
Implement a refined search function for multiple models without Rails5 gem.
I want to add a browsing function with ruby on rails
Let's create a TODO application in Java 6 Implementation of search function
[Implementation procedure] Create a user authentication function using sorcery in Rails
[Ruby on Rails] Search function (not selected)
Do a prefix search in Swift's Firestore
How to implement search functionality in Rails
[Rails] Function restrictions in devise (login / logout)
How to insert a video in Rails
[Rails withdrawal] Create a simple withdrawal function with rails
Make a login function with Rails anyway
Steps to set a favicon in Rails
Implemented follow function in Rails (Ajax communication)
Add binding.pry (rails)
[Rails 6] Ranking function
[Rails] Category function
Group_by in Rails
Rails follow function
Build a bulletin board API with authentication authorization in Rails # 13 Add authentication header
[Rails] Notification function
[Rails] Implementation of search function using gem's ransack
Add .gitignore when creating a project in Xcode
Convert to a tag to URL string in Rails
Posting function implemented by asynchronous communication in Rails
Implement star rating function using Raty in Rails6
[Rails] Implementation of retweet function in SNS application
How to add a classpath in Spring Boot
[Rails] I made a draft function using enum
Rails logger Get a rough idea in 1 minute
How to conditionally add html.erb class in Rails
How to implement a like feature in Rails
How to easily create a pull-down in Rails
Add a project in any folder with Gradle
(Ruby on Rails6) Creating data in a table
Simple notification function in Rails (only when followed)
[Rails / JavaScript / Ajax] I tried to create a like function in two ways.
Build a bulletin board API with authentication authorization in Rails # 17 Add administrator privileges
Do something like a JS immediate function in Ruby