我正在尝试构建一个呼叫跟踪应用程序来学习 twilio 和 rails。现在,我正在添加购买电话号码的功能。
我想在我的创建操作中执行 if/else 语句,如果电话号码已保存到数据库中,并且实际购买了 twilio 号码,则执行此操作,否则执行此操作。
但是,twilio 数字不会返回 true 或 false。这是他们在文档中所说的:
HTTP POST
为您的帐户购买一个新的电话号码。如果为您的请求找到电话号码,Twilio 会将其添加到您的帐户中,并向您收取电话号码第一个月的费用。如果 Twilio 找不到与您的请求匹配的电话号码,您将收到带有 Twilio 错误代码 21452 的 HTTP 400。
所以,我想做的是构建一个 if 语句,如果返回是 HTTP 400,那么执行 x.
这是我目前拥有的代码
#This saves the Twilio number to the database, and buys it.
def create
@user = current_user
@phone = @user.phones.build(params[:phone])
client = Twilio::REST::Client.new(@user.twilio_account_sid, @user.twilio_auth_token)
number = client.account.incoming_phone_numbers.create({:phone_number => @phone.twilio_number, :voice_url => VOICE_URL, :voice_method => "POST"})
if @phone.save && number.true
flash[:success] = "Phone Number Created!"
redirect_to user_path
else
render new_phone_path
flash[:error] = "It looks like there were errors with the submission"
end
end
我需要更改 && number.true 以引用 HTTP 400 错误吗?
我不确定这是否可能——所以如果你有想法,我会全神贯注:)。