我目前正在我的应用程序中使用Pg gem搜索。现在,我想使用pg搜索来搜索学校和教师两个模型的数据。我创建了一个欢迎页面,让观众在那里使用搜索功能。但是,目前我想知道如何在观众输入查询后检索结果,例如,如果他们输入教师或学校的特定名称,我想要返回该教师/学校的链接,以便用户可以单击并查找有关该学校/教师的更多信息。 这是我的文件:
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