首页 > 解决方案 > Ruby 错误:未定义的局部变量或方法“c”

问题描述

我正在开发一个 ruby​​ 应用程序,但遇到一个奇怪的错误,我认为这可能是由 activerecord/activemodel 引起的。错误如下:

Error on PaymentTransaction::Normal: undefined local variable or method `c' for #<Deposits::Mobitglobal:0x0055995b000b78>
/home/deploy/peatio/vendor/bundle/ruby/2.2.0/gems/activemodel-4.0.12/lib/active_model/attribute_methods.rb:439:in `method_missing'
/home/deploy/peatio/vendor/bundle/ruby/2.2.0/gems/activerecord-4.0.12/lib/active_record/attribute_methods.rb:168:in `method_missing'

我曾尝试使用 ruby​​-2.3.0 作为建议的解决方案,认为该版本中的 activerecord/activemodel 处理方式不同。但结果是一样的。

该过程从文件开始:coin_rpc.rb


require 'net/http'
require 'uri'
require 'json'

class CoinRPC

  class JSONRPCError < RuntimeError; end
  class ConnectionRefusedError < StandardError; end

  def initialize(uri)
    @uri = URI.parse(uri)
  end

  def self.[](currency)
    c = Currency.find_by_code(currency.to_s)
    if c && c.rpc
      name = c.family || 'BTC'
      "::CoinRPC::#{name}".constantize.new(c.rpc)
    end
  end

  def method_missing(name, *args)
    handle name, *args
  end

  def handle
    raise "Not implemented"
  end
  class BTC < self
    def handle(name, *args)
      post_body = { 'method' => name, 'params' => args, 'id' => 'jsonrpc' }.to_json
      resp = JSON.parse( http_post_request(post_body) )
      raise JSONRPCError, resp['error'] if resp['error']
      result = resp['result']
      result.symbolize_keys! if result.is_a? Hash
      result
    end
    def http_post_request(post_body)
      http    = Net::HTTP.new(@uri.host, @uri.port)
      request = Net::HTTP::Post.new(@uri.request_uri)
      request.basic_auth @uri.user, @uri.password
      request.content_type = 'application/json'
      request.body = post_body
      http.request(request).body
    rescue Errno::ECONNREFUSED => e
      raise ConnectionRefusedError
    end

    def safe_getbalance
      begin
        getbalance
      rescue
        'N/A'
      end
    end
  end

  class ETH < self
    def handle(name, *args)
      post_body = {"jsonrpc" => "2.0", 'method' => name, 'params' => args, 'id' => '1' }.to_json
      resp = JSON.parse( http_post_request(post_body) )
      raise JSONRPCError, resp['error'] if resp['error']
      result = resp['result']
      result.symbolize_keys! if result.is_a? Hash
      result
    end
    def http_post_request(post_body)
      http    = Net::HTTP.new(@uri.host, @uri.port)
      request = Net::HTTP::Post.new(@uri.request_uri)
      request.basic_auth @uri.user, @uri.password
      request.content_type = 'application/json'
      request.body = post_body
      http.request(request).body
    rescue Errno::ECONNREFUSED => e
      raise ConnectionRefusedError
    end

    def safe_getbalance
      begin
        (open(@uri.host + '/cgi-bin/total.cgi').read.rstrip.to_f)
      rescue
        'N/A'
      end
    end
  end
end

该错误似乎出现在上述文件中的两个定义之间:

def self.[](currency)
    c = Currency.find_by_code(currency.to_s)
    if c && c.rpc
      name = c.family || 'BTC'
      "::CoinRPC::#{name}".constantize.new(c.rpc)
    end
  end

  def method_missing(name, *args)
    handle name, *args
  end

文件 about 调用“family”名称,在本例中为以下文件中的“BTC”:currency.yml

- id: 9
  key: mobitglobal
  code: mbg
  symbol: "฿"
  coin: true
  family: BTC
  quick_withdraw_max: 1000
  rpc: http://username:password@127.0.0.1:11111
  blockchain: http://cryptoid.net/explorer/MBGL?txid=#{txid}
  address_url: http://cryptoid.net/explorer/MBGL?txid=#{address}

此时调用应该寻找“family”,然后如果设置为 BTC,则继续 coin_rpc.rb 中的操作:

class BTC < self
    def handle(name, *args)
      post_body = { 'method' => name, 'params' => args, 'id' => 'jsonrpc' }.to_json
      resp = JSON.parse( http_post_request(post_body) )
      raise JSONRPCError, resp['error'] if resp['error']
      result = resp['result']
      result.symbolize_keys! if result.is_a? Hash
      result
    end
    def http_post_request(post_body)
      http    = Net::HTTP.new(@uri.host, @uri.port)
      request = Net::HTTP::Post.new(@uri.request_uri)
      request.basic_auth @uri.user, @uri.password
      request.content_type = 'application/json'
      request.body = post_body
      http.request(request).body
    rescue Errno::ECONNREFUSED => e
      raise ConnectionRefusedError
    end

    def safe_getbalance
      begin
        getbalance
      rescue
        'N/A'
      end
    end

出于安全原因,一些信息被混淆了。(用户名密码)

预期结果为空,表示没有错误。但相反,我得到了错误:

Error on PaymentTransaction::Normal: undefined local variable or method `c' for #<Deposits::Mobitglobal:0x0055995b000b78>
/home/deploy/peatio/vendor/bundle/ruby/2.2.0/gems/activemodel-4.0.12/lib/active_model/attribute_methods.rb:439:in `method_missing'
/home/deploy/peatio/vendor/bundle/ruby/2.2.0/gems/activerecord-4.0.12/lib/active_record/attribute_methods.rb:168:in `method_missing'

任何帮助或洞察我可能出错的地方将不胜感激

标签: activerecordactivemodelruby-2.2

解决方案


推荐阅读