当前位置: 首页 > 知识库问答 >
问题:

使用Rails和Twilio发送SMS消息

薛欣荣
2023-03-14

我是ruby和ruby on rails的新手,在设置Twilio发送短信时遇到了麻烦。

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'
#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个答案

阎璞瑜
2023-03-14

出现错误的原因是您没有要呈现的视图,例如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
 类似资料: