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.
By the way, the table is as follows.
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).
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 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 %>
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