[RUBY] How to implement search functionality in Rails

This time, I will introduce how to implement the search function in Rails.

environment

Premise

--Explain as a function to search articles on blog sites --The article table is posts --Post columns are title and body --Display search form and search results on the article list screen (index.html.erb)

Set up search routing

routes.rb


get 'search' => 'posts#search'

Create a search form on the article list screen

html:index.html.erb


<div class="search-form">
  <%= form_with url: search_path, method: :get, local: true do |f| %>
    <%= f.text_field :keyword, value: @keyword %>
    <%= f.submit "Search" %>
  <% end %>
</div>
<div class="post-list">
  <% @posts.each do |post| %>
    <%= post.title %>
  <% end %>
</div>

Create search method

Describe the following in post.rb.

post.rb


def self.search(keyword)
  where(["title like? OR body like?", "%#{keyword}%", "%#{keyword}%"])
end

OR outputs the article if the search keyword partially matches either title or body. (Use AND if you want to output only when both title and body are hit.)

Added search action

posts_controller.rb


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

This completes the search function.

References

-[Rails] How to search across multiple columns with one search form
https://qiita.com/bSRATulen2N90kL/items/6a7c99bf3641ac6838fb

Recommended Posts

How to implement search functionality in Rails
How to implement ranking functionality in Rails
How to implement a like feature in Rails
[Rails] How to implement scraping
How to implement guest login in 5 minutes in rails portfolio
How to implement a like feature in Ajax in Rails
Introduce devise in Rails to implement user management functionality
How to write a date comparison search in Rails
Rails learning How to implement search function using ActiveModel
[Rails] How to write in Japanese
How to introduce jQuery in Rails 6
[Rails] How to implement star rating
How to install Swiper in Rails
How to implement date calculation in Java
How to implement Kalman filter in Java
How to change app name in rails
How to use custom helpers in rails
How to insert a video in Rails
How to use MySQL in Rails tutorial
How to implement coding conventions in Java
[rails] How to configure routing in resources
How to implement image posting using rails
How to implement asynchronous processing in Outsystems
How to use credentials.yml.enc introduced in Rails 5.2
How to write Rails
[Rails] Implement search function
How to uninstall Rails
Implement markdown in Rails
[Rails] How to use select boxes in Ransack
How to translate Rails into Japanese in general
How to prevent direct URL typing in Rails
[Rails] How to implement unit tests for models
How to separate .scss by controller in Rails
How to conditionally add html.erb class in Rails
How to easily create a pull-down in Rails
How to use JQuery in js.erb of Rails6
[Rails] How to easily implement numbers with pull-down
How to implement optimistic locking in REST API
[Ruby on Rails] How to install Bootstrap in Rails
How to make a follow function in Rails
[Rails] How to use PostgreSQL in Vagrant environment
How to check Rails commands in the terminal
How to implement Pagination in GraphQL (for ruby)
How to implement search function with rails (multiple columns are also supported)
How to implement a slideshow using slick in Rails (one by one & multiple by one)
How to implement more than one top page in Rails breadcrumb trail
How to implement gem "summer note" in wysiwyg editor in Ruby on Rails
Implement application function in Rails
How to implement UICollectionView in Swift with code only
How to implement login request processing (Rails / for beginners)
How to set the display time to Japan time in Rails
[Rails] How to search by multiple values ​​with LIKE
Implement post search function in Rails application (where method)
[Rails] Implement User search function
[Ruby on Rails] How to write enum in Japanese
Implement follow function in Rails
[Rails] How to use enum
[Rails, JS] How to implement asynchronous display of comments
[Rails] How to install devise
[Rails] How to use enum
How to read rails routes