But in the context of a web application this just isn’t very “Rails” is it? Are we mixing views and controllers? Where’s the separation of concerns? What about localisation?

If you’ve thought this, or even if you’re just interested in better ways to send SMS messages in your Rails application, then the Textris gem may be for you. It makes working with SMS messages as easy as working with emails using Action Mailer.

Let’s take a look at what it can do.

Working with Textris

We’re going to take an example Rails application and replace the existing message sending code with Textris to see how it can tidy up our application. For this blog post, we’ll use a tutorial on sending instant lead alerts as our base. The application is a landing page for real estate. When a user expresses interest in a house, the real estate agent is instantly alerted about it via SMS. This application is written using Rails 4, but Textris works just as well with the latest Rails 5 too.

To hack on the app along with this post, you’ll need a few things:

Ruby and Bundler installed

A Twilio account (sign up for a free Twilio account if you don’t already have one) and a Twilio number that can send SMS messages

Now, clone or download the application repo from GitHub and follow the instructions in the README to get the app set up. Use your own mobile number as the AGENT_NUMBER so you will get the alerts. Once it’s setup you will see a page like this:

Fill in some details in the form and submit. You will receive a message alerting you about the request with all the details you entered. Pretty sweet right.

Let’s take this app and see what it would look like using Textris.

Setting up Textris

If you take a look around the application you’ll see that the NotificationsController ( app/controllers/notifications_controller.rb ) is where interested parties submit their details and where we send the SMS to the real estate agent. That uses a class called MessageSender ( lib/message_sender.rb ) which creates a Twilio::REST::Client and sends the message.