我正在处理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
关于文件的文件名为“家族”名称,在以下文件中为“ 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}
此时,呼叫应在寻找“家庭”,然后将其设置为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
出于安全原因混淆了一些信息。 (用户名/密码)
预期结果为null,表示没有错误。但是相反,我得到了错误:
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'
对于我可能会出错的任何帮助或见解,将不胜感激