This time I used ransack to implement the search function. I was a little worried, so I will write an excerpt.
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