[RUBY] Create an or search function with Ransack.

usage environment

rails 6.0.2
ubuntu (WSL)

procedure

gem installation

gem 'ransack'

Don't forget bundle.

Search for content

It is assumed that post_controller has already been created.

posts_controller.rb


  def index
    unless params[:q].blank?
       #Split the entered word with a space
      split_keyword = params[:q][:content_cont].split(/\p{blank}/)
    end
    @q = Post.ransack(content_cont_any: split_keyword)
    @posts = @q.result
  end

Check if there is an input (search word). I think it would be an error without this.

unless params[:q].blnak?

Search form

posts/index.html.slim


 = search_form_for @q, class: 'mb-5 search-container' do |f|
   .form-group.row
     = f.search_field :content_cont, placeholder: "Please enter a keyword", class: "form-control"
     = f.submit class: "btn btn-outline-primary"

Recommended Posts

Create an or search function with Ransack.
[For those who create portfolios] Search function created with ransack
Let's make a search function with Rails (ransack)
Create an EC site with Rails 5 ⑩ ~ Create an order function ~
Search function using [rails] ransack
Create an EC site with Rails 5 ⑨ ~ Create a cart function ~
Use ransack Search function implementation
Implement search function with form_with
Create an immutable class with JAVA
Create an app with Spring Boot 2
Create pagination function with Rails Kaminari
Create an excel file with poi
Create an app with Spring Boot
[Rails] A memo that created an advanced search form with ransack
Let's create an instance with .new yourself. .. ..
Create an infinite scroll with Infinite Scroll and kaminari
[Java] Create an executable module with Gradle
OR search with Spring Data Jpa Specification
[Rails withdrawal] Create a simple withdrawal function with rails
Create a simple search app with Spring Boot
How to search multiple columns with gem ransack
Implement search function with Rakuten Books (DVD) API
Create an EC site with Rails5 ⑤ ~ Customer model ~
[Swift] Create an image selection UI with PhotoKit
Create an HTTPS file server for development with ring-jetty-adapter
Create an EC site with Rails5 ⑦ ~ Address, Genre model ~
Category search with ransack using categories created with Active hash
Create an EC site with Rails5 ④ ~ Header and footer ~
Create an E2E test environment with Docker x Cypress
Create a user with an empty password on CentOS7
Create an EC site with Rails5 ⑥ ~ seed data input ~
Create a Service with an empty model Liferay 7.0 / DXP
Try create with Trailblazer
[Rails] Implement search function
Implementation of search function
A new employee tried to create an authentication / authorization function from scratch with Spring Security
Rails search function implementation
Search function [copype implementation]
[Rails] Create an application
With Kotorin ―― 7. Scoping Function
Serverless Function with Micronaut
Maybe it works! Create an image with Docker and share it!
When you want to explicitly write OR or AND with ransack
[Rails] Search from multiple columns + conditions with Gem and ransack
[Java] How to search for a value in an array (or list) with the contains method