[RUBY] Search function using [rails] ransack

This time I used ransack to implement the search function. I was a little worried, so I will write an excerpt.

To search the data saved by ActiveHash

This is the challenge this time. Well, when I think about it later, I could do it normally .. I will record it for the time being.

gem 'ransack'

After installation, do bundle install to restart the server.

First, define it like this in the application controller

application_controller.erb



def search
      @search= Pet.ransack(params[:q]) #Generate search object
      @[email protected]
    end

I am searching with this. I'm looking for a pet in the Pet table using the key (: q). @search= Pet.ransack(params[:q])

search.html.erb


<%= search_form_for @search,url: search_pets_path do |f| %>

<div class="search-field">
        <%= f.label :bleed_id_eq, 'Type selection' %>
        <%= f.collection_select :bleed_id_eq, Bleed.all, :id, :name, include_blank: 'unspecified'%>
        </div>

Note url: search_pets_path If this is not done, an error will occur.

_eq is a method to perform a search that meets the conditions. Do not mistake the third argument as: id.

It's done. It's pretty easy. w I was able to do it while applying various arguments and exploring lol

Recommended Posts

Search function using [rails] ransack
[Rails] Implementation of search function using gem's ransack
[Rails] Implement search function
Rails search function implementation
Let's make a search function with Rails (ransack)
Rails fuzzy search function implementation
[Rails] Implement User search function
Use ransack Search function implementation
[Rails 6] Implementation of search function
Ajax bookmark function using Rails
Rails learning How to implement search function using ActiveModel
Add a search function in Rails.
[Rails] Tag management function (using acts-as-taggable-on)
[Ruby on Rails] Search function (not selected)
[Rails] Create an evaluation function using raty.js
[Rails 6] Ranking function
[Rails] Category function
Rails follow function
Create an or search function with Ransack.
Multiple image upload function using Rails Carrierwave
[Rails] Notification function
[Rails 6] Implementation of inquiry function using Action Mailer
Create authentication function in Rails application using devise
[Rails] Implementation of image enlargement function using lightbox2
Implement partial match search function without using Ransuck
Implement star rating function using Raty in Rails6
[Rails] I made a draft function using enum
[Note] Summary of rails login function using devise ①
Rails hashtag search implementation
Implementation of search function
[Rails] Implemented hashtag function
[rails] tag ranking function
Search function [copype implementation]
[Rails] Set validation for the search function using Rakuten API (from the implementation of Rakuten API)
Implement post search function in Rails application (where method)
[Rails] Test of star evaluation function using Raty [Rspec]
Category search with ransack using categories created with Active hash
[For Rails beginners] Implemented multiple search function without Gem
[Rails] Implementation of multi-layer category function using ancestry "Preparation"
[Rails] Implementation of multi-layer category function using ancestry "seed"
[Ruby on Rails] Search function (model, method selection formula)
Try to implement tagging function using rails and js
Implement application function in Rails
[For those who create portfolios] Search function created with ransack
[Rails] Implementation of multi-layer category function using ancestry "Editing form"
Introduced graph function with rails
Try using view_component with rails
Ruby on Rails Email automatic sending function setting (using gmail)
Implementation of sequential search function
SNS authentication using Rails google
Implement follow function in Rails
[Rails] Save images using carrierwave
[Rails] Implementation of multi-layer category function using ancestry "Creation form"
Japaneseize using i18n with Rails
[Rails] Japanese localization using rails-i18n
[Rails] Creating a search box
[Rails] Implementation of category function
[Rails] Implementation of tagging function using intermediate table (without Gem)
[Rails] Function to search and list products from multi-level categories
[Rails] Implement event end function (logical deletion) using paranoia (gem)
Rails ~ Understanding the message function ~