Rails - ArgumentError(错误的参数数量(给定1,预期为0)):

时间:2017-09-12 15:26:51

标签: ruby-on-rails ruby twilio

我试图从twilio API接收短信。我生成了一个单独的回复控制器,它不会处理我的路由或资源中的任何其他内容。它使用post方法与twilio进行通信。我得到错误:

"ArgumentError (wrong number of arguments (given 1, expected 0)):"

replycontroller.rb

class ReplyController < ApplicationController

  require 'twilio-ruby'

  skip_before_action :verify_authenticity_token

  def hart1

    twiml = Twilio::TwiML::Response.new do |r|
      r.Message 'The Robots are coming! Head for the hills!'
    end

    content_type 'text/xml'
    twiml.text
  end

end

这是我的路线

Rails.application.routes.draw do
  resources :posts
  resources :phones
  resources :users
  root 'home#index'

  post "/reply/hart1" => "reply#hart1"

end

我的印象是我不正确地路由这个。 Heroku控制台也给我一个500错误,所以我知道它可以解决我的问题。

2 个答案:

答案 0 :(得分:1)

使用&twillio-ruby发送消息&#39;你的代码应该是这样的

# put your own credentials here
account_sid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
auth_token = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'

# set up a client to talk to the Twilio REST API
@client = Twilio::REST::Client.new account_sid, auth_token

# Send SMS message
@client.api.account.messages.create(
  from: '+FROMNUMBER',
  to: '+TONUMBER',
  body: 'The Robots are coming! Head for the hills!'
)

在您的控制器中它看起来像

require 'twilio-ruby'
class ReplyController < ApplicationController
  skip_before_action :verify_authenticity_token

  def hart1
    send_text_message
    content_type 'text/xml'
  end

  private

  def send_text_message
    # put your own credentials here
    account_sid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    auth_token = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'

    # set up a client to talk to the Twilio REST API
    @client = Twilio::REST::Client.new account_sid, auth_token

    # Send SMS message
    @client.api.account.messages.create(
     from: '+FROMNUMBER',
     to: '+TONUMBER',
     body: 'The Robots are coming! Head for the hills!'
    )
  end
end

&#39; twillo-ruby&#39;的文档在这里:https://github.com/twilio/twilio-ruby

答案 1 :(得分:0)

我走了nzajt路线,提到你发送常规文本而不是Twilio提到的回复。路由很好。从twilio到我的服务器上的所有路径都打了一针,我能够从其有效载荷中取出我想要的所有参数。多谢你们。