Hello I can not understand what the cause of the error when searching the Sphinx. Controller:
class SearchesController < ApplicationController
def show
@results = Search.get_results(params[:query], params[:context])
end
end
Model:
class Search < ApplicationRecord
CONTEXTS = ['Questions', 'Answers', 'Comments', 'Users']
def get_results(query, context)
query = ThinkingSphinx::Query.escape(query)
klasses = [context.singularize.constantize] if CONTEXTS.include?(context)
@results = ThinkingSphinx.search(query, classes: klasses, order: 'model_order ASC') if query.present?
end
end
Error: NoMethodError in SearchesController#show,
undefined method `get_results' for Search(id: integer, created_at: datetime, updated_at: datetime):Class
when used in self in the method
def self.get_results(query, context)
# ...
end
the error will be: undefined method `gsub' for nil:NilClass in string
query = ThinkingSphinx::Query.escape(query)
I am a beginner ... but I can not understand .. maybe sphinx installed incorrectly
Thanks in advance!
These are two different bugs.
The first one is that you are defining an instance method, but using it on the class (instead of on an instance). A class method starts
self
egand can be called on the main class eg
Search.my_method_name
instead of a specific instance of the search class. An example of calling an instance method with the same name would be:SO, your first error message tells you "there is no method called
get_results
on theSearch
class" - and this is because you have defined it asdef get_results
instead ofdef self.get_results
So, then you tried adding
self.
and this error went away.This then exposed the next bug - which is completely unrelated to the first one.
The second error complains that you are calling
gsub
on nil.the
gsub
is probably something happening inside of theThinkingSphinx::Query.escape(query)
call.and you can't (easily) change anything inside of another library... but you can still glean useful information from the error message. In this case, the error is complaining that you're calling a method on
nil
... that means that something isnil
that it was expecting to be not-nil.Given this is a search-query thing... my best guess would be that it is assuming that the
query
variable is some kind of string... containing a search-query.It probably is expecting that query * NOT * to be a nil.
and yet in this instance it is.
What if...
a) somebody hits the "go" button without entering a search-query?
b) you accidentally made a typo in the parameter name in your form and so nothing is actually coming through for
params[:query]
?c) the nesting of your params is slightly different to what you expect (eg
params[:search_form][:query]
)Firstly - you should double-check that your forms are sending through the
params
that your controller actually expects. You can do this by looking in the logs -> either in the terminal/console window, or in your logfiles (usuallylog/development.log
) and watching what happens when you hit the submit-button on your search form. The logs will list the parameters that come through when you submit that form and you can double-check it's exactly what you expect.Secondly: you probably should not actually do the searching if no query comes through - people will accidentally hit the submit button even when they haven't typed something - and it'd be a nicer GUI if your app didn't explode when that happens :)
You could do something like this:
Just make sure that your view explicitly checks for nil and doesn't explode in that case.