Twilio employee here. There have been a bunch of changes to Rails since this original question was posted, and I wanted to share how you might tackle this problem using Rails 4, Concerns and the Twilio Ruby gem.
In the code sample below, I define the controller in /controllers/voice_controller.rb
and include a Concern called Webhookable. The Webhookable Concern lets us encapsulate logic related to Twilio webhooks (setting the HTTP response header to text/xml, rendering TwiML, validating that requests originate from Twilio, etc) into a single module.
require twilio-ruby
class VoiceController < ApplicationController
include Webhookable
after_filter :set_header
# controller code here
end
The Concern itself lives in /controllers/concerns/webhookable.rb
and is fairly simple. Right now it simply sets the Content-Type to text/xml for all action and provides a method to render the TwiML object. I haven t included the code to validate that requests are originating from Twilio, but that would be easy to add:
module Webhookable
extend ActiveSupport::Concern
def set_header
response.headers["Content-Type"] = "text/xml"
end
def render_twiml(response)
render text: response.text
end
end
Finally, here s what your reminder
action might look like using the Twilio gem to generate TwiML and using the Concern to render this object as text:
def reminder
response = Twilio::TwiML::Response.new do |r|
r.Gather :action => BASE_URL + /directions , :numDigits => 1 do |g|
g.Say Hello this is a call from Twilio. You have an appointment
tomorrow at 9 AM.
g.Say Please press 1 to repeat this menu. Press 2 for directions.
Or press 3 if you are done.
end
end
render_twiml response
end