ruby-on-rails - 使用 Rails 和 Twilio 发送短信

标签 ruby-on-rails ruby sms twilio

我是 ruby​​ 和 ruby​​ on rails 的新手,在设置 Twilio 以发送短信时遇到问题。

到目前为止,我的 Rails 应用程序中有以下代码:当我发送 SMS 消息时,出现以下错误:

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 Controller

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

最佳答案

您收到该错误的原因是您没有要呈现的 View ,例如app/views/twilio/send_text_message.html.slim

如果代码在没有中断的情况下到达操作的结尾,它将尝试呈现关联的 View 。

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

关于ruby-on-rails - 使用 Rails 和 Twilio 发送短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23416636/

相关文章:

ruby-on-rails - 测试 twilio API

ruby-on-rails - 使用载波和雾将 Rails 图像上传到 S3

ruby-on-rails - 将变量从 View 传递到 View 外的模型

ruby - 在 ruby​​ 中比较 YAML 文件而不加载

android - 对默认短信应用程序的 API 支持

android - 重启后短信广播接收器不工作

css - 粘性页脚不会粘在动态 Rails 内容下面

ruby-on-rails - 如何在保留所有者和权限的同时从数据容器装载卷?

c# - 如何使用 C# 和 twilio API 发送短信

html - 如何去除 adsense 广告背景颜色