Chapter 8. Improving Forms

Now that you can safely get information between your users and your applications, it’s time to examine some ways to do it better. Here are a few more features to explore:

  • Supporting file uploads, a common website feature that steps outside of the simple form field to database column mapping

  • Designing form builders, which make it easier to create forms that look the way you think they should, not the way Rails does it by default

Once you’ve figured out these pieces, you’ll have a reasonably complete understanding of the options Rails offers for creating classic web applications. Ajax still lies ahead, but the basics are still useful for a wide variety of situations.

Adding a Picture by Uploading a File

Since we’re building a collection of people, it might be nice to know what they look like. Adding file uploads to Rails applications requires making changes in several different places:

  • The form for creating and editing a person needs a file upload field.

  • The model representing person data needs to handle the file data.

  • A new migration needs to add a field for the file extension, because pictures come in different formats.

  • The view that shows a person should display the picture, too!

One key piece of a Rails application is missing here: the controller. The controller doesn’t actually need to do anything more than it is already doing: passing data between the view and the model. One more piece of data, even a big chunk like a photo file, isn’t going to make a difference ...

Get Learning Rails: Live Edition 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.