[RUBY] [Rails] Search from multiple columns + conditions with Gem and ransack

Summary of search conditions

I implemented it by searching from multiple columns using Gem ** ransack ** in the flea market app.

If there is a keyword in either the title of the product or the text and the specified conditions are met, the product will be hit. The view of the search bar is below.

スクリーンショット 2020-11-10 11.33.46.png

By the way, the table is as follows.

スクリーンショット 2020-11-10 11.34.29.png

Category ... genre Product status ... status Shipping cost ... bear Days until shipping ... day Price ... price

is. "Category", "Product status", "Shipping cost", and "Days to ship" are stored in the table only by id using ActiveHash. I would like to perform a search that can narrow down the hits to all the above items + keywords (product title or text content).

Description of Controller

item_controller


class ItemsController < ApplicationController
  # index,show,Since the search window header is installed on the search page, it is executed at the time of that action.
  before_action :search_product, only: [:index, :show, :search]

  #abridgement

  private

 def search_product
    @p = Item.ransack(params[:q])  #Generate search object
    @results = @p.result
  end
end

: q in (params [: q]) is the default parameter key for the search parameter. The following are synonymous, but you can use the default ransack method if search is already defined elsewhere. Item.ransack(params[:q]) Item.search(params[:q])

@ p = Item.ransack (params [: q]) After creating the search object here, I put the search results in @results with @ results = @ p.result.

Search bar description

Search view


# @Put the search information in p and pass it to the controller.
<%= search_form_for @p, url: search_items_path do |f| %>
  <div class = 'nav-up'>
# :title_or_text_cont is text_Description of whether the characters entered in field are included in title or text
    <%= f.text_field :title_or_text_cont, placeholder: "Search by keyword", class: "input-box" %>
    <button class="search-button">
      <%= image_tag("search.png ", class:"search-icon")  %>
    </button>
  </div>
  <div class='nav-down'>
    <div class='label-select'>
      <%= f.label :genre_id_eq, 'Category', class: 'label' %>
      <%= f.collection_select :genre_id_eq, Genre.where.not(id: 0), :id, :name,  include_blank: 'unspecified', class: 'search-select' %>
    </div>
    <div class='label-select'>
      <%= f.label :status_id_eq, 'Product condition', class: 'label' %>
      <%= f.collection_select :status_id_eq, Status.where.not(id: 0), :id, :name,  include_blank: 'unspecified', class: 'search-select' %>
    </div>
    <div class='label-select'>
      <%= f.label :bear_id_eq, 'Shipping cost', class: 'label' %>
      <%= f.collection_select :bear_id_eq, Bear.where.not(id: 0), :id, :name,  include_blank: 'unspecified', class: 'search-select' %>
    </div>
    <div class='label-select'>
      <%= f.label :day_id_eq, 'Days to ship', class: 'label' %>
      <%= f.collection_select :day_id_eq, Day.where.not(id: 0), :id, :name,  include_blank: 'unspecified', class: 'search-select' %>
    </div>
    <div class='label-select'>
      <%= f.label :price, 'price', class: 'label' %>
      <%= f.radio_button :price_lteq, '' %>
unspecified
      <%= f.radio_button :price_lteq, '1000' %>
1000 yen or less
      <%= f.radio_button :price_lteq, '2500' %>
2500 yen or less
      <%= f.radio_button :price_lteq, '5000' %>
5000 yen or less
      <br>
    </div>
  </div>
<% end %>

View to display search results

ruby:search.html.erb


<% if @results.present? %>
  <ul class='item-lists'>
    <% @results.each do |item| %>
      <%= render partial: "shared/item", locals: {item: item} %>
    <% end %>
  </ul>
<% else %>
  <div>
    <p>There is no matching product. Let's search by changing the conditions!</p>
  </div>
  <ul class='item-lists'>
    <% Item.all.each do |item| %>
      <%= render partial: "shared/item", locals: {item: item} %>
    <% end %>
  </ul>
<% end %>

With the above, you can narrow down the products that match the search results. I will give a detailed explanation at a later date.

Recommended Posts

[Rails] Search from multiple columns + conditions with Gem and ransack
How to search multiple columns with gem ransack
How to implement search function with rails (multiple columns are also supported)
Let's make a search function with Rails (ransack)
[Rails] How to search by multiple values ​​with LIKE
Implementation of multiple word search, multiple model search, and multiple tag search (acts-as-taggable-on) of ransack
[Rails] Function to search and list products from multi-level categories
Use multiple databases with Rails 6.0
Implement a refined search function for multiple models without Rails5 gem.
Search and execute method by name from instance with processing (java)
Implemented hashtag search like Instagram and Twitter in Rails (no gem)
[Rails] A memo that created an advanced search form with ransack
[Rails] Keyword search in multiple tables
Timeless search with Rails + JavaScript (jQuery)
[Rails 6] Register and log in with Devise + SNS authentication (multiple links allowed)
[Rails] Manage multiple models using devise gem
[Rails] Benefit from rubycop with minimal effort
[Rails] Let's manage constants with config gem
[Rails] Book search with Amazon PA API
Create an or search function with Ransack.