[RUBY] [Rails] Implement User search function

Overview

While creating a bulletin board site in a Rails project, I had the opportunity to implement a User search feature. I will leave the procedure here as a memorandum.

Add routing

Edit config / routes.rb so that GET / users / search is routed to the search action of the Users controller.

config/routes.rb


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

Edit users controller

Create a search action. Since the search string is input to the search_keyword field, get it with params [: search_keyword].

app/controllers/users_controller.rb


def search
    if params[:search_keyword].present?
      @users = User.where('name LIKE ?', "%#{params[:search_keyword]}%")
    else
      @users = User.none
    end
  end

Add view

Create app / views / users / search.html.erb.

erb:views/users/search.html.erb


<h1>User search</h1>
<%= form_tag search_users_path, method: :get do %>
  <%= text_field_tag :search_keyword %>
  <%= submit_tag "Search", username: :nil, class: "button is-info" %>
<% end %>
<%= render 'users/users', users: @users %>

I already created app / views / users / _users.html.erb that contains User information, so I called it with render.

Complete

スクリーンショット 2020-09-19 10.44.17.png

When you search, it looks like this. スクリーンショット 2020-09-19 10.56.58.png

You have successfully implemented the User search function in your Rails project. Thank you very much.

Recommended Posts

[Rails] Implement User search function
[Rails] Implement search function
Rails search function implementation
Implement application function in Rails
Search function using [rails] ransack
Implement follow function in Rails
[Rails 6] Implementation of search function
[Rails] Implement image posting function
Implement search function with form_with
Implement post search function in Rails application (where method)
Implement user follow function in Rails (I use Ajax) ②
Implement user follow function in Rails (I use Ajax) ①
Rails learning How to implement search function using ActiveModel
Add a search function in Rails.
[Rails] Implementation of user withdrawal function
Implement simple login function in Rails
Implement CSV download function in Rails
Implement user registration function and corporate registration function separately in Rails devise
[Ruby on Rails] Search function (not selected)
How to implement search functionality in Rails
Implement a refined search function for multiple models without Rails5 gem.
[Rails 6] Ranking function
Implement Rails pagination
[Rails] Category function
Rails follow function
[Rails] Notification function
How to implement search function with rails (multiple columns are also supported)
[Rails] Implementation of search function using gem's ransack
Use [Rails] devise Guest user function (for portfolio)
Implement user edit / update function without using devise
Implement partial match search function without using Ransuck
Implement star rating function using Raty in Rails6
Let's make a search function with Rails (ransack)
Implement search function with Rakuten Books (DVD) API
Rails hashtag search implementation
Implementation of search function
[Rails] Implemented hashtag function
[rails] tag ranking function
Implement Rails account BAN
Search function [copype implementation]
[Rails] Implement rake task
Implement markdown in Rails
[Rails] Implement credit card registration / deletion function in PAY.JP
Introduce devise in Rails to implement user management functionality
[For Rails beginners] Implemented multiple search function without Gem
[Ruby on Rails] Search function (model, method selection formula)
Try to implement tagging function using rails and js
[Ruby on Rails] How to implement tagging / incremental search function for posts (without gem)
Introduced graph function with rails
Implementation of sequential search function
Rails Posts and User Linkage
Use ransack Search function implementation
Implement login function in Rails simply by name and password (1)
Implement LTI authentication in Rails
[Rails] Creating a search box
Implement login function in Rails simply by name and password (2)
[Rails] Implementation of category function
[Rails] Function to search and list products from multi-level categories
[Rails] Implement event end function (logical deletion) using paranoia (gem)
Rails ~ Understanding the message function ~
[Ruby on Rails] Implement login function by add_token_to_users with API