Search function [copype implementation]

Introduction

Implement the search function with the minimum required code. The explanation is good, so it's for those who want to move it anyway.

Operating environment

$ ruby -v
ruby 2.5.7p206 (2019-10-01 revision 67816) [x86_64-linux]

$ rails -v
Rails 5.2.4.3

code

/app/models/user.rb


def self.search(search)
  User.where(['name LIKE ?', "%#{search}%"])
end

/app/controllers/users_controller.rb


def index
  @users = User.all
end

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

/app/views/users/index.html.erb


<%= form_with(url: search_path, method: :get, local: true) do |f| %>
  <%= f.text_field :search %>
  <%= f.submit "Search" %>
<% end %>

<% @users.each do |user| %>
  <%= user.name %>
<% end %>

/app/config/routes.rb


get "search" => "users#search"

Reference article

[Rails] Implement search function without gem

・ I have omitted the explanation this time, so if you want to know more, I think the following article is easy to understand. [Rails] Thorough explanation of ransack and search function without gem !! Read this to become a search function master!

・ This time, I searched only by name, but when I want to search by both name and nickname [Rails] How to search across multiple columns with one search form

-Implemented by partial match search this time, but when you want to perform prefix match, suffix match, exact match search Rails-Ambiguous search for characters using LIKE clause (if you want to search for words containing specific characters)

Recommended Posts

Search function [copype implementation]
Rails search function implementation
Rails fuzzy search function implementation
Implementation of sequential search function
Use ransack Search function implementation
[Rails 6] Implementation of search function
DM function implementation
Rails hashtag search implementation
[Rails] Implementation of search function using gem's ransack
Comment function (Ajax) implementation
Follow function (Ajax) implementation
[Rails] Implement search function
Image preview function implementation
Implementation of search function Learning memo (portfolio creation)
Implementation of pagination function
[Rails] Implement User search function
Search function using [rails] ransack
Implementation of like function (Ajax)
Implementation of image preview function
Implementation of category pull-down function
Login function implementation with rails
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
Implement search function with form_with
[Rails 6] Pagination function implementation (kaminari)
[Rails] Implementation of CSV import function
[Rails] Asynchronous implementation of like function
Add a search function in Rails.
[Rails] Implementation of image preview function
[Rails] Implementation of user withdrawal function
[Rails] Implementation of CSV export function
[Rails] gem ancestry category function implementation
[Ruby on Rails] Comment function implementation
[Rails 6] Like function (synchronous → asynchronous) implementation
[Rails] Comment function implementation procedure memo
Error encountered in tagging function implementation
Implementation of like function in Java
[Ruby on Rails] Search function (not selected)
Implementation of user authentication function using devise (2)
Implementation of user authentication function using devise (1)
Rails [For beginners] Implementation of comment function
Where the follow function implementation is stuck
Rails Basic CRUD function implementation procedure scaffold
[Rails 6] Implementation of SNS (Twitter) sharing function
Implementation of user authentication function using devise (3)
[Vue.js] Implementation of menu function Implementation version rails6
[Ruby on rails] Implementation of like function
[Java] New Yahoo! Product Search API Implementation
Create an or search function with Ransack.
[Vue.js] Implementation of menu function Vue.js introduction rails6
Login function implementation by Spring Security (securityConfig)
[Rails] Set validation for the search function using Rakuten API (from the implementation of Rakuten API)