如何使用elasticsearch / searchkick在我的导航栏上运行搜索栏并在特定页面中显示结果? [英] How to use elasticsearch/searchkick to run a search bar on my navbar and show results in a specific page?

查看:171
本文介绍了如何使用elasticsearch / searchkick在我的导航栏上运行搜索栏并在特定页面中显示结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚完成了几个处理搜索的教程。我想将我的搜索栏放在导航栏上,以便每当有人搜索结果时,我将被带到我的书籍文件夹中的search.html.erb(类似于SO的工作原理)。我遇到了一个问题,但每当我进行搜索时,都被带到我的书页上[ http:// localhost:3000 / books?utf8 =%E2%9C%93& q = travel] ,没有显示任何结果,只是我的标准索引显示我所有的书,从来没有被带到我的搜索结果页。我列出了我下面的所有相关代码。如果有人能帮助我,会非常感激。谢谢你们这么多!



Book.rb

 code> class Book< ApplicationRecord 
has_many:likes,:counter_cache => true
has_many:users,through::likes

searchkick
end

books_controller.rb

  class BooksController< ApplicationController 
before_action:authenticate_user !, only:[:new,:create]
before_action:set_book,only:[:show,:edit,:update,,destroy,,share]

def index
@books = Book.all
end

def search
@booksearches = Book.search(params.fetch(:q,* ))
end

private
def set_book
@book = Book.find(params [:id])
end

def book_params
params.require(:book).permit(:title,:author,:avatar)
end

application_controller.rb

  class ApplicationController< ActionController :: Base 
protect_from_forgery with::exception
def search
@booksearches = Book.search(params.fetch(:q,*))
end
end

application.html.erb

 < body style =background-color:#f5f8fa; padding-top:70px; class =<%= @body_class%>> 
< nav class =navbar navbar-default navbar-fixed-top>
< div class =collapse navbar-collapseid =bs-example-navbar-collapse-1>
< ul class =nav navbar-nav>
< li>
<%= form_tag books_path,方法::get do%>
<%= text_field_tag:q,nil,占位符:搜索...%>
<%end%>
< / li>
< / ul>
< / div>
< / nav>
< / body>


解决方案

研究,通过尝试和错误入侵,但终于可以让它上班如果其他人有类似的问题,我已经概述了下面对我有用的。



三个步骤


  1. 将路径文件添加到您的路线文件中,以便用户可以访问搜索结果页。

post'books / search'=>'books#search',as:'search_books'



2.在你的application.html上调整form_tag .erb可以正确地指向搜索结果页面



<%= form_tag search_books_path do%>
<%= text_field_tag:q,nil,占位符:搜索...%>
<%end%>


  1. 在您的search.html .erb确保列出您的搜索结果

<%@ booksearches.each do | booksearch | %>< div>< h3><%= link_to booksearch.title,booksearch%>< / h3>< / div>
<%end%>


I just finished going over a few tutorials dealing with search. I wanted to put my search bar on my navbar so that whenever someone searches the results I will be taken to my search.html.erb within my books folder (similar to how SO works). I ran into an issue though, every time I did a search I was taken to my books page [http://localhost:3000/books?utf8=%E2%9C%93&q=travel] with no results being shown just my standard index showing all my books and was never taken to my search results page. I've listed all my relevant code below. If anyone can help me out would be super grateful. Thank you guys so much!

Book.rb

class Book < ApplicationRecord
has_many :likes, :counter_cache => true
has_many :users, through: :likes

searchkick
end

books_controller.rb

class BooksController < ApplicationController
before_action :authenticate_user!, only: [:new, :create]
before_action :set_book, only: [:show, :edit, :update, :destroy, :share]

def index
@books = Book.all
end

def search
@booksearches = Book.search(params.fetch(:q, "*"))
end

private
  def set_book
  @book = Book.find(params[:id])
end

def book_params
  params.require(:book).permit(:title, :author, :avatar)
end

application_controller.rb

class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
def search
@booksearches = Book.search(params.fetch(:q, "*"))
end
end

application.html.erb

<body style="background-color: #f5f8fa; padding-top: 70px;" class="<%= @body_class %>">
<nav class="navbar navbar-default navbar-fixed-top">
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li>
            <%= form_tag books_path, method: :get do %>
                <%= text_field_tag :q, nil, placeholder: "Search..." %>
            <% end %>
        </li>         
      </ul>
</div>
</nav>
</body>

解决方案

Researched, hacked through trial and error but was finally able to get it to work. If anyone else has a similar issue, I've outlined what worked for me below.

Three Steps

  1. Add a post to your routes file so that a user can go to the search results page.

post 'books/search' => 'books#search', as: 'search_books'

2.Adjust your form_tag on your application.html.erb so that it can properly point to your search results page

<%= form_tag search_books_path do %> <%= text_field_tag :q, nil, placeholder: "Search..." %> <% end %>

  1. In your search.html.erb make sure to list out your search results

<% @booksearches.each do |booksearch| %><div><h3><%= link_to booksearch.title, booksearch %></h3></div> <% end %>

这篇关于如何使用elasticsearch / searchkick在我的导航栏上运行搜索栏并在特定页面中显示结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆