[RUBY] Rails search function implementation

Implemented search function in Rails

Set up a search window with form_tag in View

<div class="search_field">
    <p>Search:</p>
    <%= form_tag(posts_path, :method => 'get') do %>
      <%= text_field_tag :search, "",  {class: "search_field_type"}%>
      <%= submit_tag 'Search', :name => nil, class: "search_field_btn" %>
    <% end %>
  </div>

Please change the path part of form_tag (posts_path,: method =>'get') according to the class name.

Set the method search for search in Model

post.rb

def self.search(search)
    if search
      Post.where(["title LIKE ?", "%#{search}%"])
    else
      Post.all
    end
  end

Set to hit if the searched character string is applicable even in a part of the title column.

For more information, you can find out what it means by looking at the where clause of SQL.

https://www.wakhok.ac.jp/biblion/1994/DB/subsection2.4.3.5.html

Controller adjustment

posts_controller.rb

def index
    @posts = Post.all.includes(:user).order(id: "DESC").search(params[:search])
  end

This will put the articles that hit the search in @posts.

All you have to do is display the list on the View side.

Recommended Posts

Rails search function implementation
Rails fuzzy search function implementation
[Rails 6] Implementation of search function
Rails hashtag search implementation
[Rails] Implement search function
Implementation of search function
Search function [copype implementation]
[Rails] Implementation of search function using gem's ransack
[Rails] Implement User search function
Search function using [rails] ransack
Implementation of sequential search function
Use ransack Search function implementation
[Rails] Implementation of category function
Login function implementation with rails
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
[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] About implementation of like 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
DM function implementation
[Rails 6] Ranking function
[Rails] Category function
Rails follow function
[Rails] Notification function
[Ruby on Rails] Search function (not selected)
[Ruby on Rails] Follow function implementation: Bidirectional
Rails [For beginners] Implementation of comment function
Rails Basic CRUD function implementation procedure scaffold
[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
Rails6 countdown timer implementation
[rails] gem'payjp' implementation procedure
Implementation of Ruby on Rails login function (Session)
[Rails 6] Implementation of inquiry function using Action Mailer
Comment function (Ajax) implementation
[Rails] Implemented hashtag function
[rails] tag ranking function
[Rails] Implementation of retweet function in SNS application
Image preview function implementation
Ruby on Rails Email automatic sending function implementation
Let's make a search function with Rails (ransack)
Implementation of search function Learning memo (portfolio creation)
Implementation of pagination function
[Rails] Set validation for the search function using Rakuten API (from the implementation of Rakuten API)
Ruby on Rails <2021> Implementation of simple login function (form_with)
Implement post search function in Rails application (where method)
[Rails] Implementation of drag and drop function (with effect)
Rails CRUD function implementation ② (edited and detailed this time)
[For Rails beginners] Implemented multiple search function without Gem
[Ruby on Rails] Implementation of tagging function/tag filtering function
[Rails] Implementation of multi-layer category function using ancestry "seed"