Chapter 15. Saving and Loading Data: Keep It Around

image with no caption

Your web app is just throwing users’ data away. You’ve set up a form for users to enter data into. They’re expecting that you’ll save it, so that it can be retrieved and displayed to others later. But that’s not happening right now! Anything they submit just disappears.

In this, our final chapter, we’ll prepare your app to save user submissions. We’ll show you how to set it up to accept form data. We’ll show you how to convert that data to Ruby objects, how to save those objects to a file, and how to retrieve the right object again when a user wants to see it. Are you ready? Let’s finish this app!

Saving and retrieving form data

In the last chapter, we learned how to use the Sinatra library to respond to HTTP GET requests from a browser. We built a Movie class and embedded movie data into an HTML page.

We even learned how to serve an HTML form so that users can enter new movie data.

But that’s as far as HTTP GET requests can take us. We have no way to submit that form back to the server. And we wouldn’t know how to save the form data if we got it.

In this chapter, we’re going to fix all that! We’ll learn how to take the user’s form data and convert it to Ruby objects for easy storage. We’ll also learn how to save those objects to a file and then retrieve them for display later. This chapter is going to bring ...

Get Head First Ruby now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.