2017-05-17 1 views
0

사람을 호출하는 Twilio Rails 프로젝트를 설정하려고합니다. this repo과 관련된 자습서를 따르고 있으며 기본적으로 코드베이스의 카피 본을 가지고 있습니다. 나는이 줄에서 생각하는 오류가 발생했습니다 @validator = Twilio::Util::RequestValidator.new(@@twilio_token). 내가 ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin15]Rails 5.1.0을 사용하고NoMethodError : Twilio 음성 예제에서 정의되지 않은 메서드`sort`

enter image description here

:

은 여기 내 twilio_controller.rb

class TwilioController < ApplicationController 
    # Before we allow the incoming request to connect, verify 
    # that it is a Twilio request 
    skip_before_action :verify_authenticity_token 
    before_action :authenticate_twilio_request, :only => [ 
    :connect 
    ] 

    @@twilio_sid = ENV['TWILIO_ACCOUNT_SID'] 
    @@twilio_token = ENV['TWILIO_AUTH_TOKEN'] 
    @@twilio_number = ENV['TWILIO_NUMBER'] 
    @@api_host = ENV['TWILIO_HOST'] 

    # Render home page 
    def index 
    render 'index' 
    end 

    def voice 
    response = Twilio::TwiML::Response.new do |r| 
     r.Say "Yay! You're on Rails!", voice: "alice" 
     r.Sms "Well done building your first Twilio on Rails 5 app!" 
    end 
    render :xml => response.to_xml 
    end 

    # Handle a POST from our web form and connect a call via REST API 
    def call 
    contact = Contact.new 
    contact.user_phone = params[:userPhone] 
    contact.sales_phone = params[:salesPhone] 

    # Validate contact 
    if contact.valid? 

     @client = Twilio::REST::Client.new @@twilio_sid, @@twilio_token 
     # Connect an outbound call to the number submitted 
     @call = @client.calls.create(
     :from => @@twilio_number, 
     :to => contact.user_phone, 
     :url => "#{@@api_host}/connect/#{contact.encoded_sales_phone}" # Fetch instructions from this URL when the call connects 
    ) 

     # Let's respond to the ajax call with some positive reinforcement 
     @msg = { :message => 'Phone call incoming!', :status => 'ok' } 

    else 

     # Oops there was an error, lets return the validation errors 
     @msg = { :message => contact.errors.full_messages, :status => 'ok' } 
    end 
    respond_to do |format| 
     format.json { render :json => @msg } 
    end 
    end 

    # This URL contains instructions for the call that is connected with a lead 
    # that is using the web form. 
    def connect 
    # Our response to this request will be an XML document in the "TwiML" 
    # format. Our Ruby library provides a helper for generating one 
    # of these documents 
    response = Twilio::TwiML::Response.new do |r| 
     r.Say 'FUCK.', :voice => 'alice' 
     # r.Dial params[:sales_number] 
    end 
    render text: response.text 
    end 


    # Authenticate that all requests to our public-facing TwiML pages are 
    # coming from Twilio. Adapted from the example at 
    # http://twilio-ruby.readthedocs.org/en/latest/usage/validation.html 
    # Read more on Twilio Security at https://www.twilio.com/docs/security 
    private 
    def authenticate_twilio_request 
    twilio_signature = request.headers['HTTP_X_TWILIO_SIGNATURE'] 
    # Helper from twilio-ruby to validate requests. 
    @validator = Twilio::Util::RequestValidator.new(@@twilio_token) 

    # the POST variables attached to the request (eg "From", "To") 
    # Twilio requests only accept lowercase letters. So scrub here: 
    post_vars = params.reject {|k, v| k.downcase == k} 

    is_twilio_req = @validator.validate(request.url, post_vars, twilio_signature) 

    unless is_twilio_req 
     render :xml => (Twilio::TwiML::Response.new {|r| r.Hangup}).text, :status => :unauthorized 
     false 
    end 
    end 

end 

오류 이미지입니다. 레일 5.1, ActionController::Parameters가 더 이상 Hash에서 상속 때문에

+0

그것은 정의되지 않은 메서드'sort'를 들어, authenticate_twilio_request''내부의 선 (85)에 실패했습니다. 당신의 코드를 보면서, 당신은'authenticate_twilio_request' 안에'sort'를 사용하지 않았습니다, 아마도 위의 코드를 편집 했습니까? –

+0

Rails 5.1에서'ActionController :: Parameters # sort' 같은 것이 이미 제거 된 것 같습니다. "ActionController :: Parameters'가 더 이상 해시로부터 상속받지 못하기 때문에"Rails 5.0.3에서'정렬 '을 시도해 보았습니다. 지원 중단 경고. –

+0

위의 코드에서'sort'의 흔적을 볼 수 없기 때문에 추가 검사시 https://github.com/twilio/twilio-ruby/blob/ac2680cde6913af94e24fb24ee27bb28ffa2602b/lib에서 오류의 원인이되는 행을 발견했습니다. /twilio-ruby/util/request_validator.rb:'data = url + params.sort.join' 이것은'is_twilio_req = @ validator.validate (request.url, post_vars, twilio_signature)'에 의해 호출됩니다. 따라서 제 대답을보십시오 수정 사항은 아래에서 확인하십시오. –

답변

3

귀하의 코드가 가장 가능성이 있기 때문에 gem's code의 검사에 is_twilio_req = @validator.validate(request.url, post_vars, twilio_signature)에 실패, 그것은

data = url + params.sort.join

아래 sort에 실패 이것은 같은 Hash 방법입니다 sort (see Hash docs)은 더 이상 작동하지 않습니다.

명시 적으로 해시에 params을 변환해야합니다 :

def authenticate_twilio_request 
    twilio_signature = request.headers['HTTP_X_TWILIO_SIGNATURE'] 
    @validator = Twilio::Util::RequestValidator.new(@@twilio_token) 

    # convert `params` which is an `ActionController::Parameters` object into `Hash` 
    # you will need `permit!` to strong-params-permit EVERYTHING so that they will be included in the converted `Hash` (you don't need to specifically whitelist specific parameters for now as the params are used by the Twilio gem) 
    params_hash = params.permit!.to_hash 

    post_vars = params_hash.reject {|k, v| k.downcase == k} 

    is_twilio_req = @validator.validate(request.url, post_vars, twilio_signature) 

    unless is_twilio_req 
    render :xml => (Twilio::TwiML::Response.new {|r| r.Hangup}).text, :status => :unauthorized 
    false 
    end 
end 
+0

고마워요! 집에 돌아와 집에 돌아 가면 – Sticky

+1

작품 덕분입니다. 고마워요! : D – Sticky

+0

@Sticky 문제 없습니다! :) –

관련 문제