[Rails] Creating a search box

procedure

Routing settings Controller settings view file: create search

Routing settings

Added search to profiles routing.

rutes.rb


resources :profiles do
    get :search, on: :collection
  end
search_profiles GET    /profiles/search(.:format)     profiles#search

Controller settings

This time, we assume a case where there is a partial match for: name, a case where the gender matches, or a case where both match.

controller.rb


def search
    if params[:name].present? && params[:sex].present?
      @profiles = Profile.where('name LIKE ?', "%#{params[:name]}%").where(sex: "#{params[:sex]}")
    elsif params[:name].present?
      #Partial Match
      @profiles = Profile.where('name LIKE ?', "%#{params[:name]}%")
    elsif params[:sex].present?
      @profiles = Profile.where(sex: "#{params[:sex]}")
    else
      # @profiles = Profile.none
      @profiles = Profile.none
    end
  end

Create view

search.html.erb


<h1>Search box</h1>
<%= form_with url: search_profiles_path, method: :get, local: true do |f| %>
<%= f.label :name, "name" %>
<%= f.text_field :name %>

 <%= f.label :male%><%= f.radio_button :sex, :male%>
 <%= f.label :Female%><%= f.radio_button :sex, :Female%>

<%= f.submit :search %>
<% end %>

Recommended Posts

[Rails] Creating a search box
Add a search function in Rails.
[Rails] Creating a new project with rails new
Rails Basics of creating a new application
Let's make a search function with Rails (ransack)
(Ruby on Rails6) Creating data in a table
[Rails] Creating a breadcrumb trail using Gem gretel
[For beginners] Procedure for creating a controller using rails
Rails hashtag search implementation
[Rails] Implement search function
Creating a local repository
Rails search function implementation
Creating a test case
How to write a date comparison search in Rails
How to get started with creating a Rails app
[Rails] Launch a new project
Rails fuzzy search function implementation
[Rails] Implement User search function
Search function using [rails] ransack
Creating a batch of Liferay
Creating a calendar using Ruby
Post a video on rails
Establish a search bar in Rails ~ Join multiple tables to search
[Rails] Make a breadcrumb trail
Explanation of Ruby on rails for beginners ③ ~ Creating a database ~
What beginners can learn by creating a simple Rails API
[Rails] Implemented a pull-down search function for Active Hash data
Implement a refined search function for multiple models without Rails5 gem.
[Rails] Avoid SELECT issued when creating a model with belongs_to defined!
(Ruby on Rails6) Creating a database and displaying it in a view
Naming convention when creating a new controller or model with rails
Creating a mixed conditional expression of Rails if statement and unless
[Rails] A memo that created an advanced search form with ransack
Creating a new user with rails was angry with unknown attribute'password' for User.
[Self-study] Creating a portfolio (original app) after completing the Rails tutorial
[Rails] Keyword search in multiple tables
Preparing to create a Rails application
[Rails Tutorial Chapter 5] Create a layout
Creating a timer app with a muddy
Create a new app in Rails
Creating an Elasticsearch Plugin Series (2) Search
Implement a contact form in Rails
Timeless search with Rails + JavaScript (jQuery)