Implementation of search function

Introduction

When creating a personal application, implement a search function so that the content of posted articles can be displayed by searching.

Routing settings

This time, we will not transition to the page by specifying: id, so we will use collection to set the routing.

routes.rb


resources :posts do
  get :search, on: :collection
  resource :likes, only: [:create, :destroy] -Like function implementation
end

Model settings

** LIKE clause ** The LIKE clause allows you to search for strings. Used with the where method.

Execution example Details
where('title LIKE(?)', "a%") Titles starting with a
where('title LIKE(?)', "%b") Titles ending in b
where('title LIKE(?)', "%c%") Titles containing c
where('title LIKE(?)', "d_") Two-letter title starting with d
where('title LIKE(?)', "_e") Two-letter title ending in e

app/models/post.rb


def self.search(search)
  return Post.all unless search
  Post.where('body LIKE(?)', "%#{search}%")
end

Define search action in controller

post_controller.rb


def search
  @posts = Post.search(params[:keyword])
end

view implementation

It is assumed that you are doing a partial template using the ** render method **. ** post ** to the right of {post: post} is ** post ** as a variable in each method, indicating an instance of ** post **. The ** post ** on the left represents the name of the variable in the partial template.

search.html.erb


<% @posts.each do |post| %>
   <%= render partial: "post", locals: { post: post } %>
<% end %>

in conclusion

The implementation of the search function was completed relatively easily. I will implement other functions and increase what I can do. Thank you for watching to the end: grin:

Recommended Posts

Implementation of search function
Implementation of sequential search function
[Rails 6] Implementation of search function
Rails search function implementation
Implementation of pagination function
Search function [copype implementation]
[Rails] Implementation of search function using gem's ransack
Rails fuzzy search function implementation
Use ransack Search function implementation
Implementation of like function (Ajax)
Implementation of image preview function
[Rails] Implementation of category function
Implementation of category pull-down function
Implementation of search function Learning memo (portfolio creation)
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
[Rails] Implementation of CSV import function
[Rails] Asynchronous implementation of like function
[Rails] Implementation of image preview function
[Rails] Implementation of user withdrawal function
[Rails] Implementation of CSV export function
Implementation of like function in Java
DM function implementation
Implementation of GKAccessPoint
Implementation of user authentication function using devise (2)
Implementation of user authentication function using devise (1)
Rails [For beginners] Implementation of comment function
[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
[Vue.js] Implementation of menu function Vue.js introduction rails6
Rails hashtag search implementation
Implementation of Ruby on Rails login function (Session)
Implementation of flash messages
[Rails] Implementation of image enlargement function using lightbox2
Comment function (Ajax) implementation
Follow function (Ajax) implementation
[Rails] Implement search function
[Rails] Implementation of retweet function in SNS application
Image preview function implementation
[JQuery] Implementation procedure of AutoComplete function [Java / Spring]
Applied implementation of chat-space
Implementation of delete function (if you have foreign_key)
[Rails] Set validation for the search function using Rakuten API (from the implementation of Rakuten API)
[Rails] Implementation of drag and drop function (with effect)
Implementation of Ruby on Rails login function (devise edition)
[Ruby on Rails] Implementation of tagging function/tag filtering function
[Rails] Implementation of multi-layer category function using ancestry "Preparation"
[Rails] Implementation of multi-layer category function using ancestry "seed"
[Rails] Implementation of SNS authentication (Twitter, Facebook, Google) function
Addition of guest login function
Rails implementation of ajax removal
[Swift] Simple implementation of UIImageView
[Rails] Implement User search function
Search function using [rails] ransack
Practice of binary search method
[Swift] Implementation of ultra-simple billing
Combination of search and each_with_index
[Java] Implementation of Faistel Network
Implementation of XLPagerTabStrip with TabBarController