1
在此錯誤彈出之前,一切都很順利,現在我無法收到短信。以下是日誌使用Twilio試用帳戶無法收到短信
Started POST "/phone_numbers" for 127.0.0.1 at 2015-08-28 20:24:38 +0530
Processing by PhoneNumbersController#create as JS
Parameters: {"utf8"=>"✓", "phone_number"=>{"phone_number"=>"+919738437250", "user_id"=>"9"}, "commit"=>"Send PIN"}
PhoneNumber Load (0.4ms) SELECT "phone_numbers".* FROM "phone_numbers" WHERE "phone_numbers"."phone_number" = $1 AND "phone_numbers"."user_id" = $2 LIMIT 1 [["phone_number", "+919738437250"], ["user_id", 9]]
(0.1ms) BEGIN
SQL (0.2ms) UPDATE "phone_numbers" SET "pin" = $1, "updated_at" = $2 WHERE "phone_numbers"."id" = $3 [["pin", "4143"], ["updated_at", "2015-08-28 14:54:38.800401"], ["id", 51]]
(99.4ms) COMMIT
Completed 500 Internal Server Error in 4519ms (ActiveRecord: 100.1ms)
Twilio::REST::RequestError - The requested resource /2010-04-01/Accounts//Messages.json was not found:
twilio-ruby (4.2.1) lib/twilio-ruby/rest/base_client.rb:124:in `connect_and_send'
twilio-ruby (4.2.1) lib/twilio-ruby/rest/base_client.rb:54:in `block (2 levels) in <class:BaseClient>'
twilio-ruby (4.2.1) lib/twilio-ruby/rest/list_resource.rb:95:in `create'
() home/pavan/Roorah/app/controllers/phone_numbers_controller.rb:14:in `create'
以下是我的代碼。
#config/initializers/twilio.rb
path = File.join(Rails.root, "config/twilio.yml")
TWILIO_CONFIG = YAML.load(File.read(path))[Rails.env] || {'sid' => '', 'from' => '', 'token' => ''}
#config/twilio.yml
development:
from: '+1xxxxxxxxxx'
sid: 'Axxxxxxxxxxxxxxxxxxxxxxxxxe0'
token: '5xxxxxxxxxxxxxxxxxxxxxxxxxa0'
#app/controllers/phone_numbers_controller.rb
class PhoneNumbersController < ApplicationController
def new
@phone_number = PhoneNumber.new
end
def create
@phone_number = PhoneNumber.find_or_create_by(phone_number: params[:phone_number][:phone_number], user_id: params[:phone_number][:user_id])
@phone_number.generate_pin
# Instantiate a Twilio client
client = Twilio::REST::Client.new(TWILIO_CONFIG['sid'], TWILIO_CONFIG['token'])
# Create and send an SMS message
client.messages.create(
from: TWILIO_CONFIG['from'],
to: @phone_number.phone_number,
body: "Enter #{@phone_number.pin} to verify your phone number"
)
respond_to do |format|
format.js # render app/views/phone_numbers/create.js.erb
end
end
def verify
@phone_number = PhoneNumber.find_by(phone_number: params[:hidden_phone_number])
@phone_number.verify(params[:pin])
@user = User.find(params[:user_id])
@user.update(primary_phone: params[:hidden_phone_number], verified: true) if @phone_number.verify(params[:pin])
respond_to do |format|
format.js
end
end
end
可能是什麼問題?
你能告訴如何用'config_for'改變'TWILIO_CONFIG ['sid']'嗎? – Pavan
您可以使用'config_for'來代替初始程序中的代碼。你可以用'TWILIO_CONFIG = config_for(:twilio)'代替它,然後繼續正常進行。 – philnash
我應該改變'phone_numbers_controller.rb'中的任何內容嗎?我已經用'TWILIO_CONFIG = Rails.application.config_for(:twilio)'替換了初始化器中的內容,現在得到'ArgumentError - Account SID和auth token are required'錯誤。 – Pavan