[RUBY] Are you still exhausted to implement the search function? Gem'ransack' that can be implemented in an instant

Hello,

This time, Gem that can implement the search function at explosive speed

Ransack is.


No, this is really convenient. The search function is casually high level, isn't it? I was thinking, With this Gem, you can do it in about 30 minutes.

It feels like Gem is amazing again. (vocabulary)


Let's do it now


First of all
gem 'ransack'

In the Gemfile.

% bundle install

Don't forget


Go routing

 resources :books do
    collection do
      get 'search'
    end
  end

Routing is

         search_books GET      /books/search(.:format)                                                                  books#search

I feel like this.

There is a member similar to the collection

 member do
      get 'search'
 end

Allows you to include the id in the routing

         search_books GET      /books/:id/search(.:format)                                                                  books#search

This time I did this because I want to be able to search the column called tag_name in the book database.


Next we will make a view

rails g controller books  search

This will create a controller and search View.


Controller Controller ♪

books_controller.rb


class BooksController < ApplicationController
  before_action :search_book, only: [:index, :search]


  def index
   @books =Book.all
  
   @book =Book.includes(:user)
   set_book_column 
  end

 
  def search
    @results = @p.result
    @book = @results.includes(:book)
  end


  private

  def search_book
    @p = Book.ransack(params[:q]) 
  end
  def set_book_column
    @book_name = Book.select("tag_name").distinct 
   end
end

It is like this.

Book.select("tag_name").distinct

Put the element you want to search in place of tag_name


If you put before_action It loads faster than anything else.
Next is View!

books/index.html.reb


<%= search_form_for @p, url: search_books_path do |f| %>
    <%= f.label :tag_name_cont, 'Tag name' %>
    <%= f.text_field  :tag_name_cont, placeholder: "Search by tag name" %>
    <br>
    <%= f.submit 'Search' %>
<% end %>

The search_form_for method is a Lansac-specific method

The cont of tag_name_cont written on the second line is If it matches, it will be caught in the search.

That is the one.

If you set it to eq, only the exact same one will be caught.

There are several others, so take a look! Ransack Recommendation


Make a search result screen

books/search.html.erb


   <h1 class="search-forms">
search results
   </h1>
<head class ="search-book-list">
  <div class="book-chosen">
  <%#Search Applicable product list%>
   <% if @results.length !=0 %>    
    <% @results.each do |result| %>
     <br>
     <div class="sec1title">
    <div class="item-show">
          <h2 class='border01'>
            <%= result.genre.type %>
             <%= link_to "/books/#{result.id}" do %>
        <%end %>
          </h2>
        <%= image_tag result.image, id: 'slideshow' if result.image.attached? %>
        <div class="item-info" >
          <h2 id ='item-name'>
            <%= result.name %>
          </h2>
            <form class="item-content-show">
            <%= result.content %>
            </form>
        </div>
        <br />
       </div>
     </div>
    <% end %>
   <% else %>
    <br/>
There is no applicable product
   <br/>
   <br/>
<div>
   <%=link_to  "to return home",root_path%>
</div>
   <% end %>
   <br>
  </div>
  
<%#Home button%>
   <%= render "shared/sidebar" %>

 </head>

dekita!:relaxed:

With a controller

 def search
    @results = @p.result
    @book = @results.includes(:book)
  end

Since it is written like this, from @result I am taking out the value.


This is the end of the explanation. Environment Ruby on rails

Feel free to comment if you make any mistakes! !!

Recommended Posts

Are you still exhausted to implement the search function? Gem'ransack' that can be implemented in an instant
[Java] Implement a function that uses a class implemented in the Builder pattern
I implemented the multiple image upload function in Rails so that multiple images can be slid for the post
Are you still exhausted by the sample video search? A button to send FANZA videos to Slack when pressed.
Memo that transitions to the login screen if you are not logged in with devise
[Question] Can nullif be used in the count function in JPQL?
Introduction to Rakefile that can be done in about 10 minutes
Object-oriented design that can be used when you want to return a response in form format
[Behavior confirmed in December 2020] How to implement the alert display function
Determine if the strings to be compared are the same in Java
Creating an ArrayList that allows you to throw in and retrieve the coordinates of a two-dimensional plane
An active hash that can be treated as data even if it is not in the database
How to implement search function with rails (multiple columns are also supported)
A solution to an error that makes you angry that you are not following the MySQL default setting ONLY_FULL_GROUP_BY in a production environment and it is not unique.