rails中的自动完成功能未按预期工作

时间:2016-02-24 18:29:12

标签: ruby-on-rails search typeahead

我试图按照本教程

http://blog.ragnarson.com/2013/10/10/adding-search-and-autocomplete-to-a-rails-app-with-elasticsearch.html

现在这就是我所做的

      <div class="col-md-12" align="center" style="padding-top: 1%">
        <%= javascript_include_tag "//cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.9.3/typeahead.min.js" %>
        <%= form_tag search_events_path, method: :get do %>
        <%= text_field_tag :search, params[:search], placeholder: "What are you looking for?", class: "srchFld typeahead", id: "search", autocomplete: "on" %>
        <%= submit_tag "Search", id: "submit", class: "srchBtn" %>
        <% end %>
      </div>
      <script type="text/javascript" charset="utf-8">
        $(function () {
          return $('#search').typeahead({name: "search", remote: "/events/autocomplete?query=%QUERY"});
        });
      </script>

这是我认为的

我在路线文件中有这个

resources :events, only: :index do
  collection do
    post :import
    get :autocomplete # <= add this line
  end
end

现在在rails控制台中它运行正常,这就是我运行的内容和它返回的内容

2.2.3 :001 > Event.search("123", autocomplete: true).map(&:eventname)
  Event Search (69.5ms)  curl http://localhost:9200/events_development/_search?pretty -d '{"query":{"multi_match":{"fields":["eventname.autocomplete"],"query":"123","analyzer":"searchkick_autocomplete_search"}},"size":1000,"from":0,"fields":[]}'

这就是

中的内容
GET http://localhost:8000/events/autocomplete?query=123 404 (Not Found)

这就是控制器中发生的事情

Started GET "/events/autocomplete?query=123" for ::1 at 2016-02-24 18:25:57 +0000

ActionController::RoutingError (undefined method `autocomplete' for EventsController:Class):
  app/controllers/events_controller.rb:2:in `<class:EventsController>'
  app/controllers/events_controller.rb:1:in `<top (required)>'


  Rendered vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.8ms)
  Rendered vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/routing_error.text.erb (20.8ms)

控制器:

def autocomplete     渲染json:Event.search(params [:query],autocomplete:true,limit:10).map(&amp;:eventtitle) 端

0 个答案:

没有答案