NoMethodError,未定义的方法
NoMethodError, undefined method
你好,我在搜索Sphinx的时候不明白是什么原因导致的错误。
控制器:
class SearchesController < ApplicationController
def show
@results = Search.get_results(params[:query], params[:context])
end
end
型号:
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
错误:SearchesController#show 中的 NoMethodError,
undefined method `get_results' for Search(id: integer, created_at: datetime, updated_at: datetime):Class
在方法中自己使用时
def self.get_results(query, context)
# ...
end
错误将是:字符串
中 nil:NilClass 的未定义方法 `gsub'
query = ThinkingSphinx::Query.escape(query)
我是初学者...但我看不懂..可能sphinx安装不正确
提前致谢!
这是两个不同的错误。
第一个是您正在定义一个实例方法,但在 class(而不是实例)上使用它。 class 方法开始 self
eg
def self.my_method_name
并且可以在主 class 上调用,例如 Search.my_method_name
而不是搜索的特定实例 class。调用具有相同名称的实例方法的示例是:
my_search = Search.new(:some => :attributes)
my_search.my_method_name
所以,您的第一条错误消息告诉您 "there is no method called get_results
on the Search
class" - 这是因为您将其定义为 def get_results
而不是 def self.get_results
那么,您尝试添加 self.
并且此错误消失了。
然后这暴露了下一个错误 - 这与第一个完全无关。
第二个错误抱怨你在 nil 上调用 gsub
。
gsub
可能是在 ThinkingSphinx::Query.escape(query)
调用中发生的事情。
并且您不能(轻松地)更改另一个库中的任何内容...但是您仍然可以从错误消息中收集有用的信息。
在这种情况下,错误是抱怨您正在 nil
上调用方法...这意味着某些东西是 nil
它期望不是零。
鉴于这是一个搜索查询...我最好的猜测是假设 query
变量是某种字符串...包含一个搜索查询。
可能期望查询 * NOT * 为 nil。
然而在这个例子中它是。
如果……怎么办
a) 有人在没有输入搜索查询的情况下点击了 "go" 按钮?
b) 您不小心在表单的参数名称中输入了错误,因此 params[:query]
实际上什么也没有通过?
c) 您的参数嵌套与您期望的略有不同(例如 params[:search_form][:query]
)
首先 - 您应该仔细检查您的表单是否通过您的控制器实际期望的 params
发送。您可以通过查看日志 -> 在 terminal/console window 或您的日志文件(通常是 log/development.log
)中查看当您点击您的提交按钮时会发生什么搜索表单。日志将列出您提交该表单时出现的参数,您可以仔细检查它是否正是您所期望的。
其次:如果没有查询通过,您可能实际上不应该进行搜索 - 人们会不小心点击提交按钮,即使他们没有输入任何东西 - 如果你的应用程序没有输入,它会是一个更好的 GUI'发生这种情况时不会爆炸:)
你可以这样做:
def self.get_results(query, context)
return nil unless query.present?
# ... the rest of the method here
end
只需确保您的视图明确检查 nil 并且在这种情况下不会爆炸。
你好,我在搜索Sphinx的时候不明白是什么原因导致的错误。 控制器:
class SearchesController < ApplicationController
def show
@results = Search.get_results(params[:query], params[:context])
end
end
型号:
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
错误:SearchesController#show 中的 NoMethodError,
undefined method `get_results' for Search(id: integer, created_at: datetime, updated_at: datetime):Class
在方法中自己使用时
def self.get_results(query, context)
# ...
end
错误将是:字符串
中 nil:NilClass 的未定义方法 `gsub'query = ThinkingSphinx::Query.escape(query)
我是初学者...但我看不懂..可能sphinx安装不正确
提前致谢!
这是两个不同的错误。
第一个是您正在定义一个实例方法,但在 class(而不是实例)上使用它。 class 方法开始 self
eg
def self.my_method_name
并且可以在主 class 上调用,例如 Search.my_method_name
而不是搜索的特定实例 class。调用具有相同名称的实例方法的示例是:
my_search = Search.new(:some => :attributes)
my_search.my_method_name
所以,您的第一条错误消息告诉您 "there is no method called get_results
on the Search
class" - 这是因为您将其定义为 def get_results
而不是 def self.get_results
那么,您尝试添加 self.
并且此错误消失了。
然后这暴露了下一个错误 - 这与第一个完全无关。
第二个错误抱怨你在 nil 上调用 gsub
。
gsub
可能是在 ThinkingSphinx::Query.escape(query)
调用中发生的事情。
并且您不能(轻松地)更改另一个库中的任何内容...但是您仍然可以从错误消息中收集有用的信息。
在这种情况下,错误是抱怨您正在 nil
上调用方法...这意味着某些东西是 nil
它期望不是零。
鉴于这是一个搜索查询...我最好的猜测是假设 query
变量是某种字符串...包含一个搜索查询。
可能期望查询 * NOT * 为 nil。
然而在这个例子中它是。
如果……怎么办
a) 有人在没有输入搜索查询的情况下点击了 "go" 按钮?
b) 您不小心在表单的参数名称中输入了错误,因此 params[:query]
实际上什么也没有通过?
c) 您的参数嵌套与您期望的略有不同(例如 params[:search_form][:query]
)
首先 - 您应该仔细检查您的表单是否通过您的控制器实际期望的 params
发送。您可以通过查看日志 -> 在 terminal/console window 或您的日志文件(通常是 log/development.log
)中查看当您点击您的提交按钮时会发生什么搜索表单。日志将列出您提交该表单时出现的参数,您可以仔细检查它是否正是您所期望的。
其次:如果没有查询通过,您可能实际上不应该进行搜索 - 人们会不小心点击提交按钮,即使他们没有输入任何东西 - 如果你的应用程序没有输入,它会是一个更好的 GUI'发生这种情况时不会爆炸:)
你可以这样做:
def self.get_results(query, context)
return nil unless query.present?
# ... the rest of the method here
end
只需确保您的视图明确检查 nil 并且在这种情况下不会爆炸。