在另一个控制器中搜索

时间:2016-01-07 08:58:43

标签: ruby-on-rails searchkick

我是Ruby on rails的新手,请帮我解决我的问题。

我已经使用了gem searchkick,在控制台中我可以得到结果。

这是我的代码

route.rb

resources :m_customers

resource :patient, except: [:index, :show] do
  collection do
    get 'autocomplete'
  end
end
root :to => 'home#index'

get 'patient/PatientList', to:'patient#PatientList'
get 'patient/PatientList/autocomplete', to:'patient#autocomplete'
get 'patient/PatientHistory/:kode/:histid', to:'patient#PatientHistory', as: 'patient/PatientHistory'
get 'patient/PatientSub/:id', to:'patient#PatientSub', as: 'patient/PatientSub'
get 'patient/PatientObj/:objid', to:'patient#PatientObj', as: 'patient/PatientObj'
get 'patient/PatientAsses/:assid', to:'patient#PatientAsses', as: 'patient/PatientAsses'
get 'patient/PatientPlan/:planid', to:'patient#PatientPlan', as: 'patient/PatientPlan'

m_customer.rb =>模型

class MCustomer < ActiveRecord::Base
  self.primary_key = :Cust_ID

  searchkick match: :word_start, searchable: [:Cust_Name, :Cust_ID]
  MCustomer.search "tipping poi"
 end

patient_controller.rb

 class PatientController < ApplicationController
   require 'will_paginate/array' 
   def PatientList
      @date_now = Time.now
      @patients = TRegistration.paginate(:page => params[:page], :per_page => 7).where(:Reg_status => 'N') 
      @patient2s = TRegistration.paginate(:page => params[:page], :per_page => 7).where(:Reg_status => 'P')

      if params[:query].present?
        @MCustomers = MCustomer.search(params[:query])
      else
        @MCustomers = []
      end
   end

   def autocomplete
     render json: MCustomer.search(params[:query], autocomplete:true, limit: 10).map do |customer| {name: customer.Cust_Name, value: customer.Cust_ID}
     end
   end
 end

当我转到下面的页面时,我可以从searchkick找到结果:

     //localhost:3000/patient/PatientList/autocomplete?query=s

但是当我插入serch输入时,无法显示任何内容

PatientList.html.erb

      <div class="cust-search">
        <%= form_tag patient_PatientList_path, method: :get do %>
          <div class="form-group">
            <%= text_field_tag :query, params[:query], class: 'form-control twitter-typeahead' %>
            <%= submit_tag 'Search', class: 'btn btn-default' %>
          </div>
        <% end %>
      </div>

java script patient.js

 var ready;
  ready = function() {
  var engine = new Bloodhound({
  datumTokenizer: function(d) { 
          return Bloodhound.tokenizers.whitespace(d.name); },
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  remote: { url: '../patient/PatientList/autocomplete?query=%QUERY' }
  });

  // initialize the bloodhound suggestion engine

  var promise = engine.initialize();

  promise 
  .done(function() { console.log('success!'); })
  .fail(function() { console.log('err!'); });

  // instantiate the typeahead UI
  $( '.twitter-typeahead').typeahead(null, {
    displayKey: 'Cust_Name',
    source: engine.ttAdapter()
    });
  }

  $(document).ready(ready);
  $(document).on('page:load', ready);

请帮忙。无论如何,谢谢。

1 个答案:

答案 0 :(得分:0)

如果您想要一个有效的自动完成功能(仅限相关部分)

,这就是您的代码应该是什么样子

m_customer.rb(您应该考虑将其命名为客户)

class MCustomer < ActiveRecord::Base
   searchkick autocomplete: ['Cust_Name']
 end

您应该使用自动填充选项并提供您想要自动填充的列,例如:cust_name

patient_controller.rb(您应该考虑将其命名为customer_controller)

 class PatientController < ApplicationController

   def autocomplete
     render json: MCustomer.search(params[:query], autocomplete:true, limit: 10).map do |customer| { name: customer.Cust_Name }
     end
   end
 end

这里是格式化你的json响应

patient.js(仔细阅读并查看修改)

  var engine = new Bloodhound({
  datumTokenizer: function(d) { 
          return Bloodhound.tokenizers.whitespace(d.name); },
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  remote: { url: '/patient/autocomplete?query=%QUERY' }
  });

  var promise = engine.initialize();

  $( '.twitter-typeahead').typeahead(null, {
    name: "customer",
    displayKey: "name",
    limit: 20,
    source: engine.ttAdapter()
    });
  }

我认为这是你犯错误的地方,你应该提供正确的密钥,这样方法就会读取你应该的json并检索你想要的名字。

你应该查看这个精彩的指南,你可以阅读并只是复制粘贴适合你代码的修改: https://rubyplus.com/articles/4031-Autocomplete-using-Typeahead-and-Searchkick-in-Rails-5

希望它可以帮助任何人