2015-08-28 95 views
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 

可能是什麼問題?

回答

1

Twilio開發人員傳道士在這裏。

它看起來像你在用YAML加載你的配置的地方,帳號SID將回落到默認值,空字符串。這就是爲什麼堆棧跟蹤顯示出來沒有你的帳戶SID的網址:

Twilio :: REST :: RequestError - 請求的資源/2010-04-01/Accounts//Messages.json沒有被發現:

檢查您的配置以及您如何加載YAML以及TWILIO_CONFIG['sid']的值是多少。

此外,加載配置的更簡單的方法可能是使用Rails內置的config_for幫助程序。看看這裏的文檔:http://api.rubyonrails.org/classes/Rails/Application.html#method-i-config_for

+0

你能告訴如何用'config_for'改變'TWILIO_CONFIG ['sid']'嗎? – Pavan

+0

您可以使用'config_for'來代替初始程序中的代碼。你可以用'TWILIO_CONFIG = config_for(:twilio)'代替它,然後繼續正常進行。 – philnash

+0

我應該改變'phone_numbers_controller.rb'中的任何內容嗎?我已經用'TWILIO_CONFIG = Rails.application.config_for(:twilio)'替換了初始化器中的內容,現在得到'ArgumentError - Account SID和auth token are required'錯誤。 – Pavan