Rails gem searchkick返回false结果

时间:2016-01-01 09:16:17

标签: ruby-on-rails elasticsearch searchkick

我有教师模型,我正在使用searchkick这个模型。第一个问题是当我搜索特定的老师时,如果老师在模特中,它不仅会返回该老师,还会返回我模型中的其他老师。我在互联网上做了一些研究,找不到我的案例的解决方案。因此,我真的需要你的帮助。 以下是我的一些文件,以防你需要看到它们,我认为你这样做了。

teachers_controller.rb

class TeachersController < ApplicationController
  before_action :find_school
  before_action :find_teacher, only: [:show, :edit, :update, :destroy]

  def show
  end

  def search
    if params[:search].present?
      @teachers = @school.teachers.search(params[:search], fields: [{fullName: :exact}])
    else
      @teachers = []
    end
  end

  def new
    @teacher = @school.teachers.build
  end

  def create
    @teacher = @school.teachers.create(teacher_params)
    @teacher.save
      redirect_to(@school)
  end

  def edit
  end

  def update
    @teacher.update(teacher_params)
    redirect_to(@school)
  end

  private

  def find_school
    @school = School.find(params[:school_id])
  end

  def find_teacher
    @teacher = Teacher.find(params[:id])
  end

  def teacher_params
    params.require(:teacher).permit(:firstName, :lastName, :middleName, :department, :school_id)
  end
end

search.html.haml

.text-center
  / No search results announcement/notification
  - if @teachers.blank?
    %h2 Xin lỗi, hệ thống chúng tôi không có thông tin về giảng viên mà bạn muốn tìm.
  - else
    - @school.teachers.each do |teacher|
      %h2= link_to teacher.to_s, school_teacher_path(@school, teacher)

%br/

.text-center
  = link_to("Trang Trước", school_url(@school), class: "btn btn-default btn-xs")
  = link_to("Trang Chủ", root_path, class: "btn btn-default btn-xs")

teacher.rb

class Teacher < ActiveRecord::Base
  belongs_to :school
  has_many :ratings

  searchkick word_start: [:firstName, :lastName, :middleName]

  def name
    "#{lastName} #{middleName} #{firstName}"
  end

  def to_s
    name
  end
end

的routes.rb

Rails.application.routes.draw do
  devise_for :users

  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 'schools#index'
end

0 个答案:

没有答案