简体   繁体   English

ruby on rails通过自动完成实现搜索

[英]ruby on rails implement search with auto complete

I've implemented a search box that searches the "Illnesses" table and the "symptoms" table in my DB. 我已经实现了一个搜索框,用于搜索我的数据库中的“疾病”表和“症状”表。 Now I want to add auto-complete to the search box. 现在我想在搜索框中添加自动完成功能。

I've created a new controller called "auto_complete_controller" which returns the auto complete data. 我创建了一个名为“auto_complete_controller”的新控制器,它返回自动完成数据。 I'm just not sure how to combine the search functionality and the auto complete functionality: I want the "index" action in my search controller to return the search results, and the "index" action in my auto_complete controller to return the auto_complete data. 我只是不确定如何结合搜索功能和自动完成功能:我希望搜索控制器中的“索引”操作返回搜索结果,并且我的auto_complete控制器中的“索引”操作返回auto_complete数据。

Please guide me how to fix my html syntax and what to write in the js.coffee file. 请指导我如何修复我的html语法以及在js.coffee文件中写入的内容。 I'm using rails 3.x with the jquery UI for auto-complete, I prefer a server side solution, and this is my current code: 我正在使用rails 3.x和jquery UI进行自动完成,我更喜欢服务器端解决方案,这是我当前的代码:

main_page/index.html.erb: main_page / index.html.erb:

<p>
    <b>Syptoms / Illnesses</b>
    <%= form_tag search_path, :method => 'get' do %>
      <p>
        <%= text_field_tag :search, params[:search] %> <br/>

        <%= submit_tag "Search", :name => nil %>
      </p>
  <% end %>
</p>

auto_complete_controller.rb: auto_complete_controller.rb:

class AutoCompleteController < ApplicationController

    def index
    @results = Illness.order(:name).where("name like ?", "%#{params[:term]}%") + Symptom.order(:name).where("name like ?", "%#{params[:term]}%")

    render json: @results.map(&:name)
  end
end

search_controller.rb: search_controller.rb:

class SearchController < ApplicationController

def index
    @results = Illness.search(params[:search]) + Symptom.search(params[:search])

    respond_to do |format|
        format.html # index.html.erb
        format.json { render json: @results }
    end
 end
end

Thanks, Li 谢谢,李

我有同样的问题,不得不为它创建这个宝石: https//github.com/rayasocialmedia/rails_autocomplete

Here's how to do dynamic typeahead in Twitter-Bootstrap; 以下是如何在Twitter-Bootstrap中进行动态预测; I'm sure it's something similar for jQuery: 我确定它与jQuery类似:

https://gist.github.com/1848558 https://gist.github.com/1848558

Essentially, by listening to to non-navigational keystrokes, it triggers an AJAX partial text search to your controller. 本质上,通过监听非导航键击,它会触发对控制器的AJAX部分文本搜索。 This return data then populates the JS framework's typeahead/autocomplete data to be displayed. 然后,此返回数据将填充要显示的JS框架的预先输入/自动完成数据。 This means that you really only need the one SearchController. 这意味着你真的只需要一个SearchController。

Try rails3-jquery-autocomplete . 尝试使用rails3-jquery-autocomplete I am using it and had the same requirements as you, and they work fine together. 我正在使用它并且具有与您相同的要求,它们可以很好地协同工作。 Let me know if you need further help. 如果您需要进一步的帮助,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM