7

Does anybody have any example code on how this would work? Seems like it should be pretty straightforward, but the Twilio documentation is sparse for SMS/Rails.

I have a simple "post" model with a "body" column and "from" column. I just want to display the SMS messages in a list. The closest thing I got to work was something like this:

#posts_controller.rb
class PostsController < ApplicationController
 def new
   @post = Post.new(:body=>"?",:from=>"?")
   @post.save
 end
end

#twilio sms url: ...myappurl/posts/new

This creates a new post, but the 'from' and 'body' values are "?", obviously. How do I pass the Twilio SMS 'From' and 'Body' values into the rails controller?

Any ideas or a nudge in the right direction? Thanks!

1 Answer 1

7

Just solved it! It was as simple as I thought it was.

In my posts_controller.rb file:

def twilio_create
  @post = Post.new(:body => params[:Body], :from => params[:From])
  @post.save
end

This effectively pulls the Body and From params from Twilio. The same can be applied for other params (SmsMessageSid, AccountSid, etc).

You can see the full list of parameters sent with Twilio's request here.

2
  • 1
    Three quick notes: 1) You should adhere to standard Ruby conventions, especially when posting public code (spaces around operators/etc); 2) You can use Post.create ( <data> ) instead of using two separate calls if you aren't going to use a conditional to verify and perform an action within the method; 3) Using capitalized symbols is awkward – consider using lowercased input field names. Thanks for sharing the answer once you found it, and welcome to StackOverflow!
    – coreyward
    Jan 12, 2011 at 5:15
  • 5
    Thanks! I am planning on using conditionals within the methods , and regarding (3) - those specific capitalized symbols are required to work with Twilio. Lowercase symbols don't work - I just tested it. Jan 12, 2011 at 5:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.