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

时间:2016-09-18 17:36:30

标签: ruby-on-rails ruby-on-rails-4 twilio

我已尝试按照this教程设置基本的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)

Rake路线输出

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

1 个答案:

答案 0 :(得分:2)

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

你可以在你的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