简体   繁体   English

使用 Ransack gem 和 EasyAutoComplete 进行全局自动完成搜索(搜索查询不起作用)

[英]Global Autocomplete Search with Ransack gem and EasyAutoComplete (search query not working)

I am at my wits end on this.我对此无能为力。 I am trying to implement this tutorial for a global autocomplete search bar which searches the database for keywords typed and lists them.我正在尝试为全局自动完成搜索栏实现本教程,该搜索栏在数据库中搜索键入的关键字并列出它们。 I have seen this question , and this , and this , and this , and several others.我已经看到了这个问题,而这个这个,而,和其他几个人。 None has solved my issue.没有人解决我的问题。

The search query just does not return any results plus the autocomplete does not trigger.搜索查询不会返回任何结果,并且不会触发自动完成。 I, however, am still able to make calls to the function easyAutocomplete as shown in the official guide .但是,我仍然可以调用函数easyAutocomplete ,如官方指南中所示。 And I have no errors (at least on the surface).而且我没有错误(至少在表面上)。

Here are my code samples:这是我的代码示例:

routes.rb:路线.rb:

Rails.application.routes.draw do

  resources :photo_uploads

  #......................

  resources :posts do
    put 'publish' => 'posts#publish', on: :member
    put 'unpublish' => 'posts#unpublish', on: :member
  end
  get :search, controller: :posts
  root to: 'static_pages#home'

  get 'static_pages/about'

  get 'static_pages/contact'

end

application_controller.rb: application_controller.rb:

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_action :search


#.....................
  def search
    @q = Post.ransack(title_cont: params[:q])
    @searchpost= @q.result(distinct: true) 
    # @bodysearch = Post.ransack(body_cont: params[:q]).result(distinct: true) 

    respond_to do |format|
      format.html {}
      format.json {
        @searchpost = @searchpost.limit(6) 
        # @bodysearch = @bodysearch.limit(6)
      }
    end 
  end
end

erb file: .erb 文件:

<%= search_form_for @q do |f| %>
    <%= f.search_field :title_cont, class: " search-bar ml-3", data: { behavior: "autocomplete" } %>
     <%= f.submit %>
<% end %>

search.json.jbuilder: search.json.jbuilder:

json.posts do
  json.array!(@posts) do |post|
    json.title post.title
    json.url post_path(post)
    json.published post.published_at
  end
end

search.js:搜索.js:

document.addEventListener("turbolinks:load", function () {
  $input = $("[data-behaviour='autocomplete']")

  var options = {
    getValue: "title",
    url: function (phrase) {
      return "/search.json?q=" + phrase;

    },
    categories: [
      {
        listlocation: "posts",
        header: "<strong>Posts</strong>",
      }
    ],
    list: {
      onChooseEvent: function () {
        var url = $input.getSelectedItemData().url
        $input.val("")
        Turbolinks.visit(url)
      }
    }
  }
  $input.easyAutocomplete(options)
});

posts_controller.rb: post_controller.rb:

before_action :force_json, only: :search

# .............

private

    def force_json
      request.format = :json
    end 

I use the devise gem for authentication and cancancan for user roles/access.我使用设计 gem 进行身份验证,使用 cancancan 进行用户角色/访问。 They work just fine.他们工作得很好。 The rails version is 5.1.6 rails 版本是5.1.6

What am I doing wrong?我究竟做错了什么?

This might be a little late, but I was also trying to follow the tutorial and I had a similar issue.这可能有点晚了,但我也在尝试按照教程进行操作,但我遇到了类似的问题。 For me, I was using Rails 6 and webpacker and I had to make sure that the js and css files for easyautocomplete were in the asset pipeline and they were being linked properly.对我来说,我使用的是 Rails 6 和 webpacker,我必须确保 easyautocomplete 的 js 和 css 文件在资产管道中并且它们被正确链接。 Then once I got webpacker to compile without errors, the search bar worked.然后一旦我让 webpacker 编译没有错误,搜索栏就起作用了。

I found a solution for this.我为此找到了解决方案。

The reason was that Rails 6 significantly changed the Asset Pipeline, replacing the old way (Sprockets) with WebPacker + Yarn.原因是 Rails 6 显着改变了 Asset Pipeline,用 WebPacker + Yarn 取代了旧的方式(链轮)。 So the JS parts in this tutorial no longer works.所以本教程中的 JS 部分不再有效。

Solution: Combine that tutorial with this one: https://joelc.io/dynamic-autocomplete-rails-6解决方案:将该教程与本教程结合起来: https : //joelc.io/dynamic-autocomplete-rails-6

In more details, here are the exact steps to get this to work:更详细地,以下是使其工作的确切步骤:

  1. Follow the GoRails tutorial to the end, see that it only works partially (the JS isn't triggered, but the standalone json page displays fine)按照 GoRails 教程到最后,看到它只是部分工作(JS 没有被触发,但独立的 json 页面显示正常)
  2. Keep the code, and follow the above-linked tutorial up until step 27. You can ignore the steps beyond that because the GoRails tutorial is better in that part保留代码,并按照上面链接的教程进行操作,直到第 27 步。您可以忽略除此之外的步骤,因为 GoRails 教程在该部分更好

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

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