Ruby on rails Rails rest客户端:NoMethodError:未定义的方法'parse';

Ruby on rails Rails rest客户端:NoMethodError:未定义的方法'parse';,ruby-on-rails,rest-client,Ruby On Rails,Rest Client,我正在尝试使用Restclient与外部API通信,但在尝试在控制台中测试调用时不断出现以下错误: rest_resource = RestClient::Resource.new(uri, USERNAME, PASSWORD) #=> #<RestClient::Resource:0x007fd86992e228 @url="http://localhost:3000/api/users.json", @block=nil, @options={:user=>"myfi

我正在尝试使用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>
rest\u resource=RestClient::resource.new(uri、用户名、密码)
#=>#“myfinance”,密码=>“credit123”}>
users=rest\u resource.get
#=>NoMethodError:未定义的方法“parse”#
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

啊,可能就是这样。它发出了警告。我会试试的。谢谢谢谢,马特。看来这就是问题所在!谢谢你的帮助!