I am currently using Pg gem search in my application. Now, I want to use the pg search to search for the data across two models which are school and teacher. I created a welcome page let the audience use the search functionality there. However, at the moment I am wondering how can I retrieve the result after the audience enter the query, for example, if they type a specific name of a teacher or a school, I want to return that teacher/school with a link so that the user can click on and find out more information about that school/teacher. Here are my files:
search.haml:
.row
%h1.text-center Tìm Trường Học và Giáo Viên
= form_tag search_welcome_index_url, :id => 'custom-search-input', method: :get, class: "input-group col-md-12", role: "search" do
.input-group
= text_field_tag :search,
params[:search], class: "search-query form-control", placeholder: "Tên trường học, ví dụ: Đại Học Bách Khoa Hồ Chí Minh...."
%span.input-group-btn
= button_tag( :class => "btn btn-danger") do
.search-size
%span.glyphicon.glyphicon-search
= @results
welcome_controller.rb:
class WelcomeController < ApplicationController
def welcome
end
def search
@results = PgSearch.multisearch(params[:search])
end
end
teacher.rb:
class Teacher < ActiveRecord::Base
include PgSearch
multisearchable against: [:full_name]
belongs_to :school
has_many :ratings
end
school.rb:
class School < ActiveRecord::Base
include PgSearch
multisearchable :against => [:name]
has_many :teachers, dependent: :destroy
end
routes.rb:
Rails.application.routes.draw do
devise_for :users
resources :welcome do
collection do
get 'search'
end
end
resources :schools do
collection do
get 'search'
end
resources :teachers do
collection do
get 'search'
end
end
end
resources :teachers do
resources :ratings
end
root 'welcome#welcome'
end