gpt4 book ai didi

ruby - NoMethodError : undefined method `split' for # 法拉第

转载 作者:数据小太阳 更新时间:2023-10-29 08:10:01 28 4
gpt4 key购买 nike

我想使用 Faraday 发送一个带有 JSON 正文(用于搜索)的获取请求,但我收到了上述错误。我认为 Proc 中的 self 把事情搞砸了,但这与它无关。我正在关注 [faraday github 页面][1] 上的文档,但一直卡在这个问题上。

def perform_query
response = self.database.connection.get do |request|
request.url self.path
request.headers['Content-Type'] = 'application/json'
request.body(self.to_json)
end
end

def terms_to_json
terms_array = self.terms.keys.inject([]) do |terms_array, field|
value = self.terms[field]
terms_array.tap do |ary|
if value
ary << "\"#{field}\": \"#{value}\""
end
end
end

"{ #{terms_array.join ','} }"
end

def to_json
"{ \"queryb\" : #{self.terms_to_json} }"
end

这是堆栈跟踪,错误出现在 #perform_query 中的 get Proc 中的某处:

from /Users/chrismaddox/.rvm/gems/ruby-1.9.3-p125/gems/faraday-0.8.1/lib/faraday/request.rb:60:in `url'
from /Users/chrismaddox/.rvm/gems/ruby-1.9.3-p125/gems/faraday-0.8.1/lib/faraday/connection.rb:219:in `block in run_request'
from /Users/chrismaddox/.rvm/gems/ruby-1.9.3-p125/gems/faraday-0.8.1/lib/faraday/connection.rb:237:in `block in build_request'
from /Users/chrismaddox/.rvm/gems/ruby-1.9.3-p125/gems/faraday-0.8.1/lib/faraday/request.rb:35:in `block in create'
from /Users/chrismaddox/.rvm/gems/ruby-1.9.3-p125/gems/faraday-0.8.1/lib/faraday/request.rb:34:in `tap'
from /Users/chrismaddox/.rvm/gems/ruby-1.9.3-p125/gems/faraday-0.8.1/lib/faraday/request.rb:34:in `create'
from /Users/chrismaddox/.rvm/gems/ruby-1.9.3-p125/gems/faraday-0.8.1/lib/faraday/connection.rb:233:in `build_request'
from /Users/chrismaddox/.rvm/gems/ruby-1.9.3-p125/gems/faraday-0.8.1/lib/faraday/connection.rb:218:in `run_request'
from /Users/chrismaddox/.rvm/gems/ruby-1.9.3-p125/gems/faraday-0.8.1/lib/faraday/connection.rb:87:in `get'
from /Users/chrismaddox/Dropbox/LivingSocial/Hungry Academy/Projects/hackchat/search_ruby/elastic.rb:83:in `method_missing'
from /Users/chrismaddox/Dropbox/LivingSocial/Hungry Academy/Projects/hackchat/search_ruby/elastic.rb:112:in `perform_query'
from /Users/chrismaddox/Dropbox/LivingSocial/Hungry Academy/Projects/hackchat/search_ruby/elastic.rb:61:in `send_query'

更新:

path 方法返回搜索给定索引的路径字符串。例如 /wombats/animals/_search

Elastic::Database#path 调用 Elastic::Index#index_path:

module Elastic
ELASTIC_URL = "http://localhost:9200"


class Index
attr_reader :index_name, :type_name, :last

def initialize(type)
@index_name = "#{type}-index"
@type_name = type
@last = 0
add_to_elastic
end

def add_to_elastic
index_url = URI.parse "#{ELASTIC_URL}#{index_path}/"
Connection.new(index_url).put()
end

def index_path
"/#{self.index_name}"
end

def search_path
"#{type_path}/_search/"
end

def type_path
"#{self.index_path}/#{type_name}/"
end

end
end

最佳答案

调用 search_path = "#{type_path}/_search/"调用 type_path = "#{self.index_path}/#{type_name}/"调用 index_path = "/#{self.index_name}"

因此,如果索引名称是 wombat 且类型名称是 animal,则 search_path 的计算结果为 /wombat/animal//_search

原来这不是显示错误的问题,而是法拉第的方法不一致导致的。 Faraday::Request#urlFaraday::Request#headers 本身是 setter 方法,而 Faraday::Request#body= 是 setter body 的方法。

关于ruby - NoMethodError : undefined method `split' for #<Proc: . ..> 法拉第,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10874840/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com