我试图从我的sendtextcontroller调用send_text方法:
require 'twilio-ruby'
class SendtextController < ApplicationController
def index
end
def send_text_message
number_to_send_to = current_user.cell_phone
account_sid = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
auth_token = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
twilio_phone_number = "(954)-333-3333"
@client = Twilio::REST::Client.new account_sid, auth_token
@twilio_client.account.sms.messages.create(
:from => "+1#{twilio_phone_number}",
:to => number_to_send_to
:body => "Your bill has been added")
end
end
在我创建新账单后,在我的Billscontroller中:
class BillsController < ApplicationController
before_action :set_bill, only: [:show, :edit, :update, :destroy]
def create
@bill = Bill.new(bill_params)
#sets new bill equal to the id of the current user signed in.
@bill.user_id = current_user.id
respond_to do |format|
if @bill.save
format.html { redirect_to @bill, notice: 'Bill was successfully created.' }
format.json { render :show, status: :created, location: @bill }
else
format.html { render :new }
format.json { render json: @bill.errors, status: :unprocessable_entity }
end
end
end
end
我曾尝试在Billscontroller中的create方法中的许多不同位置调用send_text_message,并且在我的localhost上创建新帐单时没有运气。有什么建议?我究竟做错了什么?提前谢谢。
答案 0 :(得分:2)
Twilio具有您希望创建控制器以使用其REST API的用例。这主要是因为商业逻辑与其示例应用程序的关系是如何布局的。在这种情况下,只要你加载了twilio gem,就可以在你的控制器中创建一个helper方法,并从你的BillsController中调用send_text_message。所有gem都会为您提供一个包装器,以便您可以对其服务器执行GET
/ POST
个请求。
如果您有任何疑问,请与我们联系!