休息与红宝石

时间:2012-02-24 08:52:58

标签: ruby-on-rails ruby rest

在这里,我尝试使用网址传递一个ID,但该ID未附加网址...

def retrieve
    url =  "http://localhost:3000/branches/"
    resource = RestClient::Resource.new url+$param["id"]
    puts resource
end

通过

的推荐行给出ID
ruby newrest.rb  id="22"

我有这样的错误

`+': can't convert nil into String (TypeError)

但所有这些都与mozilla休息客户端有关。如何纠正这个问题?

4 个答案:

答案 0 :(得分:1)

像这样:

RestClient.get 'http://localhost:3000/branches', {:params => {:id => 50, 'name' => 'value'}}

答案 1 :(得分:0)

以下是文档中的一些示例:

private_resource = RestClient::Resource.new 'https://example.com/private/resource', 'user', 'pass'

RestClient.post 'http://example.com/resource', :param1 => 'one', :nested => { :param2 => 'two' }

只需尝试使用逗号分隔的参数或使用哈希值,然后查看您的网址为您提供的内容。

答案 2 :(得分:0)

您可以在全局ARGV数组中找到命令行参数。

如果ruby newrest.rb 22会这样做,那么

id = ARGV[0]
response = RestClient.get "http://localhost:3000/branches/#{id}"
puts response.body

答案 3 :(得分:0)

从我的观点来看,行puts resource似乎很奇怪, 但是当我们离开它的时候 我建议

def retrieve
    url =  "http://localhost:3000/branches/"
    resource = RestClient::Resource.new url
    res_with_param = resource[$param["id"]]
    puts res_with_param
end

我没有尝试过,可能会出现语法错误。 我真的是红宝石的新手。 但我希望这个想法很好。

问候, 卡茨珀