2

I have tried following this tutorial to set up a basic Twilio app but I seem to be having an issue with the routing.

I am getting this error returned when I send a message to it:

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

Here is my routes file

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

Here is my controller:

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

Update 18/09/16 19:12

I realise I should have probably made it clearer what I am trying to do here. The idea is that you text the twilio number, it sends that request to the hosted app which sends a response back through twilio to the original sender.

Error Log

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 routes output

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
thrgamon
  • 438
  • 1
  • 5
  • 14

1 Answers1

2

You don't have a root route(home page) set up.

You can do that by adding this in your 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.

In your case it will be:

root 'messages#index'

Now in your messages controller add an empty method called index like this:

def index 
end

Now create a file views/messages/index.html.erb

Problem should be solved now.

EDIT

Replace root 'messages#index' with post '/' => "messages#index", as: "root

luissimo
  • 916
  • 2
  • 10
  • 31