I teach RoR. I add to the site search field (in my case, search for movies). Searching is done using the searchkick heme. But when I try to search, it gives me the following error: Must specify fields to search. What did I do:
- Installed Elasticsearch, checked. Everything works, if you make a request for port 9200, it gives:
name "95uHTn_" cluster_name "elasticsearch" cluster_uuid "pIiQyXbfRouPMmL7oNUxMA" version number "6.1.3" build_hash "af51318" build_date "2018-01-26T18:22:55.523Z" build_snapshot false lucene_version "7.1.0" minimum_wire_compatibility_version "5.6.0" minimum_index_compatibility_version "5.0.0" tagline "You Know, for Search"
Elasticsearch - works.
- Installed the searchkick 2.4 gem
- In the model Movie added
class Movie < ApplicationRecord searchkick end
- Routs
Rails.application.routes.draw do devise_for :users resources :movies do collection do get 'search' end resources :reviews, except: [:show, :index] end root 'movies#index' end
- On the form
<%= form_tag search_movies_path, method: :get, class: "navbar-form navbar-right", role: "search" do %> <p> <%= text_field_tag :search, params[:search], class: "form-control" %> <%= submit_tag "Search", name: nil, class: "btn btn-default" %> </p> <% end %>
- And in the controller Movie
def search if params[:search].present? @movies = Movie.search(params[:search]) else @movies = Movie.all end end
- Indexed the lookup tables first so
rake searchkick:reindex CLASS=Movie
then
rake searchkick:reindex:all
In theory, everything should work. But the error Must specify fields to search is still in the controller on the line
@movies = Movie.search(params[:search])
Checked with byebug. The search parameters are passed the value from the search form. I can not understand the reason. Here from the debugger
@_params = <ActionController::Parameters {"utf8"=>"✓", "search"=>"Accident Man", "controller"=>"movies", "action"=>"search"} permitted: false>
Only here is permitted: false . But I did not set any access restrictions!
params.require(сущность).permit(поля)? And the answer to your question, in appearance, is already in Getting Started . - D-sideproducts = Product.search("apples", fields: [:name])? - D-side@movies = Movie.search(params[:search], fields: [:title])and everything works !!! - Serge Bondarenko