[RUBY] Let's make a search function with Rails (ransack)

Introduction

This time, we will create a search function using a gem called ransack. ransack is a very useful gem that makes it easy to create complex search functions with less code. Also, since you can easily create a sort function with ransack, we will also create it.

Implementation of basic search results

The target of this search is the stretch (stretchs table) list and its category (categories table) list. A search window will be provided on the stretch list screen (index view).

install gem

gem 'ransack';

Install with bundle install

View

Add a search window to the stretch index view

<div class= serch.id>
      <%= search_form_for @q, class:'form-inline' ,url: stretchs_path do |f| %>
        <%= f.search_field :action_muscles_or_name_cont, class: 'form-control input-lg', placeholder: "Enter the stretch name and muscle name" ,data: {"turbolinks" => false}%>
        <%= f.submit "Search", class: "btn btn-success btn-lg" %>
      <% end %>
</div>

Adjust with css

.form-inline .form-control {
    display: inline-block;
    width: 80%;
}

controller

class StretchsController < ApplicationController
  def index
    #Search function(Search word params[:q]Receive at @stretchs)
    @q = Stretch.all.ransack(params[:q])
    @stretchs = @q.result(distinct: true).page(params[:page])
  end
end

Receive the search word with params [: q], put the corresponding data in the search in @stretchs, It is a form to return to the index view again.

This completes the implementation of the simple search function.

Finally

This time I created a search function using ransack. If you find something wrong, please teach me.

Recommended Posts

Let's make a search function with Rails (ransack)
Make a login function with Rails anyway
Search function using [rails] ransack
Add a search function in Rails.
Let's make a Christmas card with Processing!
[Rails withdrawal] Create a simple withdrawal function with rails
Make a site template easily with Rails
Create an or search function with Ransack.
Let's make an error screen with Rails
[Rails] A memo that created an advanced search form with ransack
I tried to make a group function (bulletin board) with Rails
[Rails] Implementation of search function using gem's ransack
[Rails] Implement search function
Rails search function implementation
How to make a follow function in Rails
Create an EC site with Rails 5 ⑨ ~ Create a cart function ~
Let's make a LINE Bot with Ruby + Sinatra --Part 2
[Java basics] Let's make a triangle with a for statement
Let's make a LINE Bot with Ruby + Sinatra --Part 1
Rails fuzzy search function implementation
[Rails] Implement User search function
Introduced graph function with rails
Use ransack Search function implementation
[Rails 6] Implementation of search function
[Rails] Creating a search box
Login function implementation with rails
[Rails] Make a breadcrumb trail
Implement search function with form_with
[For those who create portfolios] Search function created with ransack
[Rails DM] Let's create a notification function when DM is sent!
I want to make a function with kotlin and java!
Let's make a simple API with EC2 + RDS + Spring boot ①
[Rails] Search from multiple columns + conditions with Gem and ransack
[Rails] Implemented a pull-down search function for Active Hash data
Make a digging maze with Ruby2D
[Rails] Make pagination compatible with Ajax
Make a slideshow tool with JavaFX
Implemented mail sending function with rails
[Rails] Creating a new project with rails new
Create pagination function with Rails Kaminari
Make a garbage reminder with line-bot-sdk-java
Make a list map with LazyMap
Timeless search with Rails + JavaScript (jQuery)
Let's unit test with [rails] Rspec!
Make a typing game with ruby
Let's make a book management web application with Spring Boot part1
Implement a refined search function for multiple models without Rails5 gem.
Let's roughly implement the image preview function with Rails + refile + jQuery.
I want to add a browsing function with ruby on rails
Let's make a book management web application with Spring Boot part3
Let's create a TODO application in Java 6 Implementation of search function
Let's make a book management web application with Spring Boot part2
[Ruby on Rails] Search function (not selected)
Let's make draw poker with ruby-Implementation 1 (card)-
How to implement search function with rails (multiple columns are also supported)
Make a family todo list with Sinatra
[Rails6] Create a new app with Rails [Beginner]
Let's make draw poker with ruby ~ Preparation ~
Let's make draw poker with ruby-Implementation 4 (Deck)-
Make a family todo list with Sinatra
[Rails] Let's manage constants with config gem