Use ransack Search function implementation

Introduction

It is a gem for implementing the search function for rails. Get data linked based on search keywords On the contrary, if there is no keyword, all are acquired.

procedure

Write ransack in Gemfile and bundle install

Gemfile


gem 'ransack'

Terminal


bundle install

next Write the process to use the function of ransack in the controller.

app/controllers/users_controller.rb


class UsersController < ApplicationController
  def index
    @q = User.ransack(params[:q])
    @users = @q.result
    #Returns the result with the result method
  end
end

next Write a search form in the view file. Search forms using ransack use a helper method called search_form_for.

:app/views/users/index.html.erb


<%= search_form_for @q do |f| %>
      <div class="d-flex">
        <div class="form-group flex-grow-1">
         #search window
          <%= f.search_field :name_cont, placeholder: "Find 〇〇", class: "form-control" %>
        </div>
        <div> 
        #Search
        <%= f.submit "Search", class: "btn btn-primary ml-1" %>
        </div>
      </div>
    <% end %>

This time, the purpose is "partial match search", so it is set to column name_cont. For "match search", use column name_eq.

Search function completed

The search function is ready! ezgif-3-d2096169635d.gif

At the end

It's a rough content, but Conclusion Thanks to the engineers who made the groundbreaking gem! : joy:

Recommended Posts

Use ransack Search function implementation
Implementation of search function
Rails search function implementation
Search function [copype implementation]
[Rails] Implementation of search function using gem's ransack
Rails fuzzy search function implementation
Search function using [rails] ransack
[Rails 6] Implementation of search function
Create an or search function with Ransack.
Let's make a search function with Rails (ransack)
Implementation of search function Learning memo (portfolio creation)
Rails hashtag search implementation
Follow function (Ajax) implementation
[Rails] Implement search function
Image preview function implementation
Implementation of pagination function
[Rails] Implement User search function
Implementation of multiple word search, multiple model search, and multiple tag search (acts-as-taggable-on) of ransack
Implementation of like function (Ajax)
Implementation of image preview function
[Rails] Implementation of category function
Implementation of category pull-down function
Login function implementation with rails
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
Implement search function with form_with
[Rails 6] Pagination function implementation (kaminari)
Let's create a TODO application in Java 6 Implementation of search function
[Rails] Implementation of CSV import function
[Rails] Asynchronous implementation of like function
Add a search function in Rails.
[Rails] Implementation of image preview function
Function is very easy to use
[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
Error encountered in tagging function implementation
Implementation of like function in Java