In the world of Rails and Active Record, validating data and storing it in a database is easy. If you’d ever built a simple site that stores data in a Google Spreadsheet then you’d quickly learn that users can enter anything (or nothing). In this post we’ll see how to validate input using part of Active Record: ActiveModel::Validations .

Spreadsheets can be databases too

In my last post we built a landing page for a new app using Sinatra and Google Spreadsheets as the database. The app needs one improvement though; users can enter any data they want in the form and it will happily submit. We need to guarantee we get some real data if we are going to contact our users when the app launches.

We should validate our input and ensure that

We get at least a name and email address

If we get an email address, it looks like a valid email address

If we get a phone number then it is a valid number

Let’s improve this app so it can do all of the above.

Getting setup

You’ll need a Google Spreadsheet and the application from the previous post if you want to follow along with the code in this tutorial. Don’t worry if you haven’t got those set up, the instructions to do so are below.

If you don’t already have a Spreadsheet setup and permissions to edit it via the API then follow this post to generate your credentials, saved as a file called client_secret.json . Make sure to give your service edit access to your spreadsheet too.

Next, clone or download the application and check out the save-data branch. This is the current state of the app from the end of the previous blog post.