如何使用elasticsearch / searchkick在我的导航栏上运行搜索栏并在特定页面中显示结果? [英] How to use elasticsearch/searchkick to run a search bar on my navbar and show results in a specific page?
问题描述
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>
研究,通过尝试和错误入侵,但终于可以让它上班如果其他人有类似的问题,我已经概述了下面对我有用的。
三个步骤
- 将路径文件添加到您的路线文件中,以便用户可以访问搜索结果页。
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%>
- 在您的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
- 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 %>
- 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屋!