【问题标题】:NoMethodError: undefined method `split' for #<Proc: ...> with FaradayNoMethodError:#<Proc: ...> 的未定义方法`split' 与 Faraday
【发布时间】:2012-06-03 23:38:13
【问题描述】:

我想使用 Faraday 发送带有 JSON 正文(用于搜索)的 get 请求,但出现上述错误。我认为 Proc 中的 self 把事情搞砸了,但这与它无关。我正在关注 [法拉第 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

【问题讨论】:

  • 从堆栈跟踪来看,您的 path 方法可能会返回一些可疑的东西。它看起来像什么?
  • 好的,我在问题中添加了路径方法
  • 最好发布路径包含的内容,而不是生成它的代码。
  • 好的更新了它的样子
  • 我还是想看看路径方法的代码

标签: ruby faraday


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-24
    • 2015-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多