2

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:

undefined method `empty?' for 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'

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

Community
  • 1
  • 1
Nick
  • 2,924
  • 4
  • 36
  • 43

2 Answers2

3

Twilio Evangelist here.

This is really interesting.

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"
)

Notice I removed the .SMS part of that.

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.

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'

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

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.

Community
  • 1
  • 1
xmjw
  • 3,154
  • 21
  • 29
0

xmjw's answer nails it. I got the same advice from Alex Chan from Twilio's support. Xmjw's answer is, however, more comprehensive as it mentions the deprecated API version.

I need to mention that I copied the code of the controller from Twilio's own blog but I renamed for no particular reason the controller method to "send". The code produced "wrong number of arguments (1 for 0)" so I added a dummy parameter to the "send" method.

I followed the advice of Xmjw and Alex Chan and re-named the controller method. I used the name "send-text_message" which was used in the code snippet on Twilio's blog. I started getting "missing template" but this is not related to twilio. Putting

head :ok

at the end of the send_text_message method removed that error message. I got that from one of the replies at Template is missing.

In short - the code snippet on Twilio's blog is somewhat outdated but the replies of Xmjw and of twilio's Alex Chan sorted it out. It is really encouraging to see such good support for Twilio's API. I am really excited to work with it - thank you, guys!

Community
  • 1
  • 1
Nick
  • 2,924
  • 4
  • 36
  • 43