Twilio Ruby SMS:未定义的方法“空?" [英] Twilio Ruby SMS: undefined method `empty?'

查看:50
本文介绍了Twilio Ruby SMS:未定义的方法“空?"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Rails 应用程序中使用 twilio.作为开发的一部分,我使用一些硬编码值设置了一个简单的控制器(来自提交 POST 请求的表单的输入基本上被忽略了).这是控制器的代码:

I am trying to use twilio in a rails application. As part of the development I set up a simple controller using some hard-coded values (the input from the form that submits the POST request is, essentially, ignored). Here is the code of the controller:

class SmsController < ApplicationController

def send (dummy)

twilio_sid = "removed-for-privacy-reasons"
twilio_token = "removed-for-privacy-reasons"
twilio_phone_number = "removed-for-privacy-reasons"
number_to_send_to = "removed-for-privacy-reasons"

@twilio_client = Twilio::REST::Client.new twilio_sid, twilio_token

@twilio_client.account.sms.messages.create(
    :from => twilio_phone_number,
    :to => number_to_send_to,
    :body => "This is a test of SendTextController"
)

end
end

控制器工作正常 - 我在我的手机上收到了硬编码的短信.但是,我在网络表单中收到以下异常:

The controller works fine - I am getting the hard-coded SMS at my mobile. However, I am getting the following exception at the web form:

未定义的方法`empty?'对于 Twilio::REST::SMS::Message:0x007f8df0e743b8

完整的堆栈跟踪在这里:

The full stack trace is here:

Application Trace | Framework Trace | Full Trace
twilio-ruby (3.12.2) lib/twilio-ruby/rest/instance_resource.rb:68:in `method_missing'
activesupport (4.1.5) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.1.5) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.1.5) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.1.5) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.1.5) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.5) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.5) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.1.5) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.1.5) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.1.5) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.1.5) lib/abstract_controller/base.rb:136:in `process'
actionview (4.1.5) lib/action_view/rendering.rb:30:in `process'
actionpack (4.1.5) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.1.5) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.1.5) lib/action_controller/metal.rb:232:in `block in action'
actionpack (4.1.5) lib/action_dispatch/routing/route_set.rb:82:in `call'
actionpack (4.1.5) lib/action_dispatch/routing/route_set.rb:82:in `dispatch'
actionpack (4.1.5) lib/action_dispatch/routing/route_set.rb:50:in `call'
actionpack (4.1.5) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.1.5) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.1.5) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.5) lib/action_dispatch/routing/route_set.rb:678:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:35:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.5) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.5) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.5) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.5) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.5) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.5) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.5) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.5) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.5) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.5) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.5) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.5) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.5) lib/rails/engine.rb:514:in `call'
railties (4.1.5) lib/rails/application.rb:144:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/Users/nnikolo/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
/Users/nnikolo/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
/Users/nnikolo/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'

另一个 SO 线程报告了类似的问题 但没有那里提供的解决方案对我有用.你能帮忙吗?

There is another SO thread reporting a similar problem but none of the solutions offered there worked for me. Can you help?

推荐答案

Twilio Evangelist 在这里.

Twilio Evangelist here.

这真的很有趣.

首先简要说明:您正在使用 SMS 资源, 已被弃用,取而代之的是 Messages.您应该使用以下内容:

First a quick side note: you are using the SMS resource, which is deprecated in favour of Messages. You should use the following:

@twilio_client = Twilio::REST::Client.new twilio_sid, twilio_token

@twilio_client.account.messages.create(
  :from => twilio_phone_number,
  :to => number_to_send_to,
  :body => "This is a test of SendTextController"
)

请注意,我删除了其中的 .SMS 部分.

Notice I removed the .SMS part of that.

其次,我承认我无法完全解释细节,我强烈怀疑使用 send 作为您的操作名称会引起各种混乱.#send 来自 Object,在这里您覆盖它,但 Rails 将它用于控制器上的各种不同的回调.我注意到您已将 (dummy) 添加到您的签名中,但是 Ruby 不支持方法重载.

Secondly, and I'll admit I can't fully explain the details, I strongly suspect using send as your action name is calling all kinds of chaos. #send comes form Object and here you're overriding it, but Rails uses it for various different callbacks on the controller. I notice you've added (dummy) to your signature, but Ruby doesn't support method overloading.

我不知道为什么,但似乎可能是问题所在.我用你的控制器创建了一个简单的 Rails 4.0.4,但我遇到了一个类似的问题:

I don't know why, but it seems that may be the problem. I created a simple Rails 4.0.4 with exactly your controller, and I had a similar problem:

undefined method `compile' for #<Twilio::REST::SMS::Message:0x007fea132761e0>

Rails.root: ~/so26076867

Application Trace | Framework Trace | Full Trace
twilio-ruby (3.12.2) lib/twilio-ruby/rest/instance_resource.rb:68:in `method_missing'
activesupport (4.0.4) lib/active_support/callbacks.rb:369:in `__define_callbacks'
activesupport (4.0.4) lib/active_support/callbacks.rb:79:in `run_callbacks'
actionpack (4.0.4) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (4.0.4) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.0.4) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.0.4) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.0.4) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.0.4) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.0.4) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.0.4) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'

我的 rails 应用程序没有 ActiveRecord,所以这可以解释一些差异.但是请注意,我们的堆栈跟踪顶部都来自 Rails 中的回调代码.当我将操作重命名为 test_sms 时,它起作用了:

My rails app doesn't have ActiveRecord, so that may explain the some of difference. But noticed that the top of our stack traces are both coming from the callbacks code in Rails. When I renamed the action to test_sms it worked:

def test_sms
  twilio_sid = "xxxx"
  twilio_token = "yyyy"
  twilio_phone_number = "zzzz"
  number_to_send_to = "...."

  @twilio_client = Twilio::REST::Client.new twilio_sid, twilio_token

  @twilio_client.account.sms.messages.create(
    :from => twilio_phone_number,
    :to => number_to_send_to,
    :body => "This is a test of SendTextController"
  )   
end

我很想知道这是否适合您.我在 Ruby 和 Rails 中看到过不小心命名方法 send 时的奇怪行为,它似乎总是引起混乱.我不确定您收到此错误的确切原因.这可能会破坏 Rails 中的某些东西.这周我要研究一下,看看我能不能弄明白.

I'd be interested to hear if this works for you. I've seen weird behaviour in Ruby and in Rails when accidentally naming a method send, it always seems to cause chaos. Exactly why you get this error, I'm not sure. This is probably breaking something in Rails. I'm going to look into this week and see if I can figure it out.

让我知道这是否有效,否则我会再试一次.

Let me know if this works, else I'll have another crack at it.

这篇关于Twilio Ruby SMS:未定义的方法“空?"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆