Ruby HTTParty没有方法错误 [英] Ruby no method error with HTTParty
问题描述
我正在尝试在Ruby中发出发布请求,但是我的行为变得不一致.我目前在OSX上使用ruby-2.0.0-p598.
I am trying to make a post request in Ruby and I am getting inconsistent behaviour. I am currently using ruby-2.0.0-p598 on OSX.
使用PRY时,我键入以下发布命令:
When using PRY and I type the following post command:
HTTParty.post(@base_uri + '/method/?argument1&api_key=' + @api_key)
我从API获得成功的响应.但是,当我通过自己的规范或在类中运行它时,会得到:
I get a successful respond from the API. However when I run it through my specs or inside the class I get:
nil:NilClass的未定义方法"+"
undefined method `+' for nil:NilClass
我知道它与加号有关,但是我发现我的行为有所不同,这很奇怪.您能否建议这样做的正确方法是什么?
I know it has to do with the plus sign, but I find it weird that I am getting a different behaviour. Can you please suggest what is the correct way of doing this?
先谢谢了.
推荐答案
看起来@base_uri和/或@api_key为空.请仔细检查它们是否使用有效字符串初始化.然后尝试
It looks like @base_uri and/or @api_key is null. Please double check if they are initialized with valid strings or not. Then try
HTTParty.post("#{@base_uri}/method/?argument1&api_key=#{@api_key}")
在这种情况下,ruby将自动尝试将@base_uri和@api_key转换为字符串,因此无需显式调用to_s方法.
In this case, ruby will automatically try to convert @base_uri and @api_key to string so no need to call to_s method explicitly.
这篇关于Ruby HTTParty没有方法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!