1
votes

new to ruby/rails here so please forgive my ignorance. Working on adding SMS capabilities to an existing app and I’ve been successful while just setting up a ruby document and sending a sms message but when I go to incorporate it in my rails app, I’m getting a little lost.

I’ve followed this document (https://www.twilio.com/blog/2012/02/adding-twilio-sms-messaging-to-your-rails-app.html) and created a SendTextController with the following code but included the account_sid and account_token in my application.yml file using ENV and figaro. In my actual file, I have my twilio phone number and the number I'd like to send it to (just blocked it out here).

Once I set this up, I’m lost at how to call this method from a view in my app?

class TwilioController < ApplicationController

  def index
  end

  def send_text_message
  number_to_send_to = "+1XXXXXXXXXX"

  twilio_sid = ENV["TWILIO_SID"]
  twilio_token = ENV["TWILIO_TOKEN"]
  twilio_phone_number = "+1XXXXXXXXXX"

  @twilio_client = Twilio::REST::Client.new twilio_sid, twilio_token

  @twilio_client.account.sms.messages.create(
  :from => "+1#{twilio_phone_number}",
  :to => number_to_send_to,
  :body => "Test Message from testing"
  )
 end
 end
1
where are stuck exactly,, you can declare routes and call itDave
I guess where I'm confused now that i have everything set up is exactly what do I need to place in my routes file and how to call it from a specific view. I'd like to add a button or link and when it's clicked it sends them a text.user3444350
Did u googled up to start with rails basic stuffDave
so in my routes do i add?user3444350
post 'twilio/send_text_message' => 'twilio#send_text_message'?user3444350

1 Answers

2
votes

Your code to send the text message looks pretty much solid - if you have that code defined in your controller, and you'd like to call it when rendering, say, the index view of your TwilioController, you should be able to call self.send_text_message().

But if you're just getting started (and presumably using Rails 4?) there's a more up-to-date tutorial that can take you through the entire integration process.