Ruby NoMethodError:未定义的方法“split';对于#<;过程:…>;与法拉第

Ruby NoMethodError:未定义的方法“split';对于#<;过程:…>;与法拉第,ruby,faraday,Ruby,Faraday,我想用Faraday发送一个带有JSON主体(用于搜索)的get请求,但是我得到了上面的错误。我原以为进程中的self把事情搞砸了,但这与此无关。我一直在关注[faraday github页面][1]上的文档,但在这一点上遇到了麻烦 def perform_query response = self.database.connection.get do |request| request.url self.path request.headers['Content-Type'

我想用Faraday发送一个带有JSON主体(用于搜索)的get请求,但是我得到了上面的错误。我原以为进程中的
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
更新:

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

弹性::数据库#路径调用弹性::索引#索引#路径:

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\u path=
“{type\u path}/\u search/”
调用type_path=
“#{self.index_path}/#{type_name}/”
调用index_path=
“/{self.index_name}”

所以,如果索引名是袋熊,类型名是动物,则搜索路径的计算结果为
/wombat/animal/\u search


事实证明,这不是显示错误的问题,而是因为法拉第的方法不一致而导致的
Faraday::Request#url
Faraday::Request#header
本身就是setter方法,而
Faraday::Request#body=
是body的setter方法。

从堆栈跟踪判断,您的
路径
方法可能返回了一些可疑的东西。它看起来像什么?好的,我在问题中添加了路径方法。发布路径包含的内容可能更好,而不是发布生成路径的代码。好的,更新了它的外观。我仍然希望看到路径方法的代码
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