Rails rest-client: NoMethodError: undefined method `parse'
Rails rest-client: NoMethodError: undefined method `parse'
我正在尝试使用 Restclient 与外部通信 API,但在控制台中尝试测试调用时不断出现以下错误:
rest_resource = RestClient::Resource.new(uri, USERNAME, PASSWORD)
#=> #<RestClient::Resource:0x007fd86992e228 @url="http://localhost:3000/api/users.json", @block=nil, @options={:user=>"myfinance", :password=>"credit123"}>
users = rest_resource.get
#=> NoMethodError: undefined method `parse' for #<String:0x007fd865eb75b8>
uri
、USERNAME
、PASSWORD
都是定义好的常量。
我无法弄清楚问题出在哪里,因为语法看起来是正确的。
您是否错误地重新定义了 URI
?
URI
是一个处理 URL 解析的 Ruby 模块。如果您在代码中重新定义该常量,如下所示:
URI = "some string"
然后你将破坏依赖于 URI 模块的所有内容,包括 RestClient。
例如:
require "rest_client"
USERNAME = "foo"
PASSWORD = "bar"
URI = "http://localhost:3000/api/users.json"
rest_resource = RestClient::Resource.new(URI, USERNAME, PASSWORD)
rest_resource.get
# => NoMethodError: undefined method `parse' for "http://localhost:3000/api/users.json":String
重新定义常量是不好的,Ruby 会警告您。例如,上面的代码会产生这个警告:
warning: already initialized constant URI
我正在尝试使用 Restclient 与外部通信 API,但在控制台中尝试测试调用时不断出现以下错误:
rest_resource = RestClient::Resource.new(uri, USERNAME, PASSWORD)
#=> #<RestClient::Resource:0x007fd86992e228 @url="http://localhost:3000/api/users.json", @block=nil, @options={:user=>"myfinance", :password=>"credit123"}>
users = rest_resource.get
#=> NoMethodError: undefined method `parse' for #<String:0x007fd865eb75b8>
uri
、USERNAME
、PASSWORD
都是定义好的常量。
我无法弄清楚问题出在哪里,因为语法看起来是正确的。
您是否错误地重新定义了 URI
?
URI
是一个处理 URL 解析的 Ruby 模块。如果您在代码中重新定义该常量,如下所示:
URI = "some string"
然后你将破坏依赖于 URI 模块的所有内容,包括 RestClient。
例如:
require "rest_client"
USERNAME = "foo"
PASSWORD = "bar"
URI = "http://localhost:3000/api/users.json"
rest_resource = RestClient::Resource.new(URI, USERNAME, PASSWORD)
rest_resource.get
# => NoMethodError: undefined method `parse' for "http://localhost:3000/api/users.json":String
重新定义常量是不好的,Ruby 会警告您。例如,上面的代码会产生这个警告:
warning: already initialized constant URI