Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails MessagesControllercreate_消息中的TypeError没有将nil隐式转换为字符串_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails MessagesControllercreate_消息中的TypeError没有将nil隐式转换为字符串

Ruby on rails MessagesControllercreate_消息中的TypeError没有将nil隐式转换为字符串,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在Rails应用程序中集成SMS API。从用户表中获取数字时,我得到的错误为: TypeError (no implicit conversion of nil into String): app/models/message.rb:16:in `+' app/models/message.rb:16:in `sms_message' 这是我的留言 您得到的数字或实体为零 为了避免这种情况,您可以将这些值转换为字符串或使用插值 转换为字符串 使用插值 Rails有一个很好的方法,

我正在Rails应用程序中集成SMS API。从用户表中获取数字时,我得到的错误为:

TypeError (no implicit conversion of nil into String):
  app/models/message.rb:16:in `+'
  app/models/message.rb:16:in `sms_message'
这是我的留言

您得到的数字或实体为零

为了避免这种情况,您可以将这些值转换为字符串或使用插值

转换为字符串

使用插值


Rails有一个很好的方法,称为“查询”。 首先,构建如下所示的params散列:

params = {username: uname, hash: hash, numbers: numbers, message: body, sender: sender}
那就给我打电话http://api.textlocal.in/send/?{params.to_query}

params.to_查询将处理nil值


但是,这只是一种编写代码的好方法。如果值为nil,您仍然会得到相同的错误。更好的方法是在进行API调用之前添加一些验证。另外,请确保您发送的密钥与API期望的密钥完全一致。

谢谢您的回答,但我仍然没有收到指定为:{errors=>[{code=>4,message=>No recipients specified}],status=>failure}ah的收件人。这可能是因为URL中的值为零,如果值为零,则可以返回
numbers = numbers.to_s
message = body.to_s # Use message instead of body while joining using +
requested_url = 
  'http://api.textlocal.in/send/?' + "username=" + uname + "&hash=" + hash + 
  "&numbers=" + numbers + "&message=" + message + "&sender=" + sender
requested_url = 
  "http://api.textlocal.in/send/?username=#{uname}&hash=#{hash}&numbers=#{numbers}&message=#{message}&sender=#{sender}"
params = {username: uname, hash: hash, numbers: numbers, message: body, sender: sender}