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

时间:2021-02-25 23:20:06

I'm trying to receive an SMS from the twilio API. I generated a separate reply controller that doesn't deal with anything else in my routes or resources. It uses a post method to communicate with twilio. Im getting the error:

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

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

replycontroller.rb

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

here are my routes

这是我的路线

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

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

end

I'm under the impression I'm routing this improperly. The Heroku console also gives me a 500 error so I know it's something fixable on my end.

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

2 个解决方案

#1


1  

To send a message with 'twillio-ruby' your code should look like this

要发送带有'twillio-ruby'的消息,您的代码应如下所示

# 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!'
)

in you controller it would look like

在你的控制器中,它看起来像

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

The documentation for 'twillo-ruby' is here: https://github.com/twilio/twilio-ruby

'twillo-ruby'的文档在这里:https://github.com/twilio/twilio-ruby

#2


0  

I wound up going the route nzajt mentioned where you send a regular text rather than the reply Twilio mentions. The routing was fine. Everything hit the post route on my server from twilio and I was able to take all the params I wanted from its payload for use. Thanks guys.

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

#1


1  

To send a message with 'twillio-ruby' your code should look like this

要发送带有'twillio-ruby'的消息,您的代码应如下所示

# 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!'
)

in you controller it would look like

在你的控制器中,它看起来像

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

The documentation for 'twillo-ruby' is here: https://github.com/twilio/twilio-ruby

'twillo-ruby'的文档在这里:https://github.com/twilio/twilio-ruby

#2


0  

I wound up going the route nzajt mentioned where you send a regular text rather than the reply Twilio mentions. The routing was fine. Everything hit the post route on my server from twilio and I was able to take all the params I wanted from its payload for use. Thanks guys.

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