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
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
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.
Feel free to comment if you make any mistakes! !!
Recommended Posts