Twilio请求路由问题 - 没有路由匹配

问题描述:

我试过以下this教程来设置一个基本的Twilio应用程序,但我似乎有路由问题。Twilio请求路由问题 - 没有路由匹配

我收到此错误返回时,我发信息给它:

ActionController::RoutingError (No route matches [POST] "/") 

这里是我的路线文件

Rails.application.routes.draw do 
    resource :messages do 
    collection do 
     post 'reply' 
    end 
    end 
end 

这里是我的控制器:

class MessagesController < ApplicationController 
skip_before_filter :verify_authenticity_token 

    def reply 
    message_body = params["Body"] 
    from_number = params["From"] 
    boot_twilio 
    sms = @client.messages.create(
     from: Rails.application.secrets.twilio_number, 
     to: from_number, 
     body: "Hello there, thanks for texting me. Your number is #{from_number}." 
    ) 

    end 

    private 

    def boot_twilio 
    account_sid = Rails.application.secrets.twilio_sid 
    auth_token = Rails.application.secrets.twilio_token 
    @client = Twilio::REST::Client.new account_sid, auth_token 
    end 
end 

更新18/09/16 19:12

我意识到我应该已经明确了我在这里要做的事情。这个想法是,你发送twilio号码,它发送请求到托管应用程序,通过twilio发回一个响应给原始发件人。

错误日志

Started POST "/" for 54.161.31.172 at 2016-09-18 19:18:00 +0100 

ActionController::RoutingError (No route matches [POST] "/"): 
    actionpack (4.2.6) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call' 
    web-console (2.3.0) lib/web_console/middleware.rb:20:in `block in call' 
    web-console (2.3.0) lib/web_console/middleware.rb:18:in `catch' 
    web-console (2.3.0) lib/web_console/middleware.rb:18:in `call' 
    actionpack (4.2.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call' 
    railties (4.2.6) lib/rails/rack/logger.rb:38:in `call_app' 
    railties (4.2.6) lib/rails/rack/logger.rb:20:in `block in call' 
    activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `block in tagged' 
    activesupport (4.2.6) lib/active_support/tagged_logging.rb:26:in `tagged' 
    activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `tagged' 
    railties (4.2.6) lib/rails/rack/logger.rb:20:in `call' 

    actionpack (4.2.6) lib/action_dispatch/middleware/request_id.rb:21:in `call' 
    rack (1.6.4) lib/rack/methodoverride.rb:22:in `call' 
    rack (1.6.4) lib/rack/runtime.rb:18:in `call' 
    activesupport (4.2.6) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call' 
    rack (1.6.4) lib/rack/lock.rb:17:in `call' 
    actionpack (4.2.6) lib/action_dispatch/middleware/static.rb:120:in `call' 
    rack (1.6.4) lib/rack/sendfile.rb:113:in `call' 
    railties (4.2.6) lib/rails/engine.rb:518:in `call' 
    railties (4.2.6) lib/rails/application.rb:165:in `call' 
    rack (1.6.4) lib/rack/lock.rb:17:in `call' 
    rack (1.6.4) lib/rack/content_length.rb:15:in `call' 
    rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service' 
    /Users/tomgamon/.rbenv/versions/2.3.0/lib/ruby/2.3.0/webrick/httpserver.rb:140:in `service' 
    /Users/tomgamon/.rbenv/versions/2.3.0/lib/ruby/2.3.0/webrick/httpserver.rb:96:in `run' 
    /Users/tomgamon/.rbenv/versions/2.3.0/lib/ruby/2.3.0/webrick/server.rb:296:in `block in start_thread' 


    Rendered /Users/tomgamon/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates 
/rescues/_trace.html.erb (2.1ms) 
    Rendered /Users/tomgamon/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates 
/routes/_route.html.erb (1.9ms) 
    Rendered /Users/tomgamon/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates 
/routes/_table.html.erb (2.6ms) 
    Rendered /Users/tomgamon/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates 
/rescues/_request_and_response.html.erb (1.3ms) 
    Rendered /Users/tomgamon/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates 
/rescues/routing_error.html.erb within rescues/layout (124.1ms) 

耙路输出

Prefix Verb URI Pattern    Controller#Action 
      root GET /      messages#index 
reply_messages POST /messages/reply(.:format) messages#reply 
     messages POST /messages(.:format)  messages#create 
    new_messages GET /messages/new(.:format) messages#new 
edit_messages GET /messages/edit(.:format) messages#edit 
       GET /messages(.:format)  messages#show 
       PATCH /messages(.:format)  messages#update 
       PUT /messages(.:format)  messages#update 
       DELETE /messages(.:format)  messages#destroy 
+0

显示您完整的routes.rb文件或者是它? – luissimo

+0

嘿,@luissimo。这是一个非常基本的,一个目的应用程序,所以这是整个路线文件。 – thrgamon

您没有设置根路径(主页)。

你可以通过在你的routes.rb中添加此:

root 'controller#action' // root to the action of the controller you want to set as the index page a.k.a the homepage. 

你的情况,这将是:

root 'messages#index' 
在您的讯息

现在控制器添加一个名为index像空的方法这样的:

def index 
end 

现在创建一个文件views/messages/index.html.erb

现在应该解决问题。

编辑

更换root 'messages#index'post '/' => "messages#index", as: "root

+0

嘿@luissimo - 不幸的是我仍然收到错误信息! – thrgamon

+0

你可以显示你的完整的错误日志(从终端) – luissimo

+0

已添加到原始问题 – thrgamon