-2
我能夠使用elasticsearch/searchkick在我的導航欄上獲得完美的基本搜索字段。目前,查詢可以執行的唯一方法是,如果用戶鍵入,然後按在他或她的鍵盤上輸入。但是,我想也允許用戶鍵入他們的查詢,然後單擊搜索圖標並執行搜索。如果有人能幫助我,我將不勝感激,下面列出了我所有的相關代碼。如何讓用戶點擊搜索圖標以使用elasticsearch/searchkick運行他的搜索查詢?
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
query = params[:q].presence || "*"
@booksearches = Book.search(query)
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 search_books_path, id:"searchform" do %>
<%= text_field_tag :q, nil, placeholder: "Search..." %>
<i class="fa fa-search" aria-hidden="true"></i>
<% end %>
</li>
</ul>
</div>
</nav>
</body>