使用Rails和Twilio发送SMS消息

时间:2014-05-01 21:08:35

标签: ruby-on-rails ruby sms twilio

我在轨道上遇到ruby和ruby的新手,并且无法设置Twilio来发送短信。

到目前为止,我的rails应用程序中有以下代码:当我发送短信时,我收到以下错误:

Missing template twilio/send_text_message, application/send_text_message with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :slim, :coffee, :haml]}.

我的初始化程序

#config/initializers/twilio.rb (removed keys)
require 'twilio-ruby'

  account_sid = "XXXXXXXXX"
  auth_token = "XXXXXXX"
  $twilio_phone_number = "+XXXXXXXXXX"

  $twilio_client = Twilio::REST::Client.new account_sid, auth_token

然后添加到路线

#routes.rb

post 'twilio/send_text_message' => 'twilio#send_text_message'

然后我有我的twilio控制器

#twilioController
class TwilioController < ApplicationController

  def index
  end

  def send_text_message
  end
end

在我看来,我已经为用户设置了一个表单,用于输入发送到我手机的消息(因为我处于试用模式且已经过验证)

#ShareWithWorld.html.slim

= form_for :twilio, url: '/twilio/send_text', method: :post do |f|
  =f.text_field "Hey"
  =f.submit "Send Text"

1 个答案:

答案 0 :(得分:4)

您收到该错误的原因是您无法渲染,例如app/views/twilio/send_text_message.html.slim

如果代码在没有中断的情况下结束操作,它将尝试渲染关联的视图。

class TwilioController < ApplicationController
  def send_text_message
    $twilio_client.account.messages.create(
      :from => '+14159341234',
      :to => '+16105557069',
      :body => ..params from form..
    )

    redirect_to root_path, notice: 'Your SMS has been sent' # This is the interrupt that will make it so it doesn't try to render the view
  end
end
相关问题