[Rails] Implement search function

Introduction

This time we will implement a search form.

This time, we will implement something that allows you to search for articles with specified keywords by searching for the title of the article. : running :: dash:

Implementation

routes

This time I will use "collection" Use "member" if you want to identify the data. If you use "member", the routing will have an "id".

It is written like this. ↓

Also, if you want to receive ": id" in params when moving the action, use member and If you don't need to go to a specific page by specifying an id, use collection to add an action.

I investigated Rails related things: Learn more about collections and members in routes.rb

And here is another reference.

Learn Rails Routing②

rails guide

config/routes.rb



 resources :albums do
    collection do
      get 'search'
    end
  end

controller

Use the "search" method for the controller and write "(params [: keyword])"

Allows you to get ": keyword".

controllers/albums_controller.rb



  def search
    @albums = Album.search(params[:keyword])
  end  

models

The search content is specified here.

The Where method and LIKE have been learned before. Click here for details

Use "LIKE" You can also use special symbols with "LIKE".

"%" Percent sign → Arbitrary character string (including blank character string) (1 or more characters)

"_" Underscore symbol → Any one character

[MySQL] Search data

Click here for easier understanding

pikawaka: [Rails] Use the where method to get the data you want!

models/album.rb



 def self.search(search)
    if search !=""    #If it is not empty, it will be searched.
      Album.where('title LIKE(?)', "%#{search}%")
    else
      Album.all
    end
  end

views

The part to be searched will be implemented using "form_with".

Write ": keyword" in the "text_field" part.

If you set the routing as the beginning, it looks like this.

Image from Gyazo

As described in the controller part, when sending

": Keyword" (word you want to search) goes into params

Search with the where method

It will be sent to "search_albums_path" in the url part.

html:views/pics/index.html.erb




     <%= form_with(url: search_albums_path, local: true, method: :get, class: "search-form") do |f| %>
       <div class="search-inside">
         <%= f.text_field :keyword, placeholder: "Search for articles", class: "search-box" %>
       </div>
       <div class="search-inside">
         <%= f.submit "Search", class: "search-btn btn" %>
       </div>
     <% end %>
    

The search part looks like this. The CSS part is omitted, but you can decorate it as you like.

Image from Gyazo

The part to be displayed is described like this.

html:views/albums/search.html.erb




     <% @albums.each do |album| %>
       <li class="list">
         <%= link_to album_path(album) do %>
           <div class="pic-img-content">
             <%= image_tag album.image, class: "pic-img" if album.image.attached? %>
           </div>
           <div class="pic-info">
             <div class="pic-title">
               <p><%= album.created_at %></p>
               <h3><%= album.title %></h3>
             </div>
             <div class="pic-text">
               <div class="text-box">
                 <p><%= album.text %></p>
               <div>
             </div>
           </div>
         <% end %>
       </li>
     <% end %>


What I had prepared for trial was displayed! !! : trophy:

Image from Gyazo

Summary

It took a while, but I was able to implement it safely.

Next time, I want to be able to do something a little different sooner. : sunny:

Recommended Posts

[Rails] Implement search function
[Rails] Implement User search function
Rails search function implementation
Implement application function in Rails
Rails fuzzy search function implementation
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)
Rails learning How to implement search function using ActiveModel
Add a search function in Rails.
Implement simple login function in Rails
Implement CSV download function in Rails
Implement Rails pagination
[Rails] Category function
Rails follow function
[Rails] Notification function
How to implement search functionality in Rails
Implement a refined search function for multiple models without Rails5 gem.
Rails hashtag search implementation
[Rails] Implementation of search function using gem's ransack
How to implement search function with rails (multiple columns are also supported)
Implementation of search function
Implement partial match search function without using Ransuck
[Rails] Implemented hashtag function
Implement star rating function using Raty in Rails6
[rails] tag ranking function
Implement Rails account BAN
Let's make a search function with Rails (ransack)
Implement search function with Rakuten Books (DVD) API
Search function [copype implementation]
[Rails] Implement rake task
Implement markdown in Rails
[Rails] Implement credit card registration / deletion function in PAY.JP
Implement user follow function in Rails (I use Ajax) ②
[For Rails beginners] Implemented multiple search function without Gem
Implement user follow function in Rails (I use Ajax) ①
[Ruby on Rails] Search function (model, method selection formula)
Try to implement tagging function using rails and js
Introduced graph function with rails
Implement LTI authentication in Rails
[Rails] Creating a search box
[Rails] Implementation of category function
Rails ~ Understanding the message function ~
Implement category function using ancestory
[Rails] (Supplement) Implemented follow function
Login function implementation with rails
[Rails] EC site cart function
Implement import process in Rails
Ajax bookmark function using Rails
[Rails] Implementation of tutorial function
[Rails] How to implement scraping
[Ruby on Rails] How to implement tagging / incremental search function for posts (without gem)
[Rails] Implementation of like function
[Rails 6] Pagination function implementation (kaminari)
Implement login function in Rails simply by name and password (1)
Implement login function in Rails simply by name and password (2)
[Rails] Function to search and list products from multi-level categories
[Rails] Implement event end function (logical deletion) using paranoia (gem)