Twilio Ruby sending SMS - undefined method `messag

2019-07-20 13:21发布

问题:

I am trying to send an SMS message from my Rails app via the Twilio API. I'm following the instructions here (https://github.com/twilio/twilio-ruby) but I can't seem to figure this one out.

Here is the code in my rails controller

require 'twilio-ruby'
account_sid = '[I inserted account_sid here]'
auth_token = '[I inserted auth_token here]'
@client = Twilio::REST::Client.new(account_sid, auth_token)
@client.account.messages.create(
  from: '+[number here]',
  to: '+[number here]',
  body: 'Hey there!'
)

Returns the follow error message (ellipsis for simple privacy reasons)

NoMethodError .... undefined method `messages' for #<Twilio::REST::Account: ....

Any help would be greatly appreciated. Thanks all!

回答1:

Have you put 'twilio-ruby' in your Gemfile?

If not, put that in and bundle

Then you can remove require 'twilio-ruby' from your controller

Here is another tutorial - https://www.twilio.com/blog/2012/02/adding-twilio-sms-messaging-to-your-rails-app.html



回答2:

Twilio evangelist here.

It may also be because we recently added the Messages resource and deprecated the SMS resource. So you may just need to update your gem to the latest version.

$ gem update twilio-ruby

Hope this helps!